473,406 Members | 2,371 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

functions and pointers

I am trying to write a simple program using 1. strings and pointers, 2. using strings and functions.
So far this is what I have come up with for the first question and I would really like some help.
Expand|Select|Wrap|Line Numbers
  1. //finding the sum and average ofthe student marks
  2. #include<iostreams.h>
  3. #include<strings.h>
  4. main(void)
  5. {
  6. int a[3],i,sum=0;
  7. float ave=0
  8. for (i=0;i<=3;i++)
  9. cout>>" Input three integers.";
  10. cin<<a[3];
  11. sum=sum_it(a);
  12. cout>>endl>>"The sum is">>sum;
  13. ave=average_it(sum);
  14. cout>>endl>>"The average is:">>ave;
  15. //output results
  16. }
  17.  
Jul 10 '08 #1
3 1089
Nepomuk
3,112 Expert 2GB
Hi verohwm! Welcome to bytes.com!

Please read the Posting Guidelines and keep to them.

As this is a C++ question, please use the C++ Forum instead of the area for Member Introductions.

Also, you didn't say, what your actual question is.

Oh, and while I'm at it anyway, please use the Code Tags when posting code: [i][code=cpp[/i]] ... [/code]

Greetings,
Nepomuk
Jul 10 '08 #2
char *str;
int n;
cout<<"enter how many letters";
cin>>n;
str=(char*)malloc(sizeof(char) * n);
gets(str);
puts(str);
Jul 11 '08 #3
JosAH
11,448 Expert 8TB
char *str;
int n;
cout<<"enter how many letters";
cin>>n;
str=(char*)malloc(sizeof(char) * n);
gets(str);
puts(str);
We don't want to spoonfeed any code here; the OP doesn't learn from it; your
spoondfeeding code is even dead wrong: you allocated one char too few. Don't
do this anymore please. Mixing C's malloc and C++ is not a good idea either:
you should use the 'new' operator for reasons explained in Bjarne Stroustrup's
great book 'The C++ Programming Language III edition'. Using the old 'gets()'
function is a real nono. Also, sizeof(char) equals one by definition.

Of course feel welcome to actually help other posters by explaining how stuff
works.

kind regards,

Jos (moderator)
Jul 11 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: Anthony Jones | last post by:
Just a bit of background: I'm one of a group of FORTRAN programmers, looking to switch to C++. We are trying to write a few simple examples to demonstrate the power of the language to our manager,...
2
by: Thomas Matthews | last post by:
Hi, I would like to create a table (or vector) of pointers to templated functions. 1. How do I declare a typedef of a pointer to a templated function? For example, I have some functions...
6
by: Melkor Ainur | last post by:
Hello, I'm attempting to build an interpreter for a pascal-like language. Currently, I don't generate any assembly. Instead, I just build an abstract syntax tree representing what I've parsed...
5
by: Steve | last post by:
Can anyone tell me if I can have an array of functions that take a variable number of parameters? If it is possible I'd like to know how to declare the array and the functions as its elements. I am...
9
by: Mikhail Teterin | last post by:
Hello! I'd like to have a variable of a pointer-to-function type. The two possible values are of type (*)(FILE *) and (*)(void *). For example: getter = straight ? fgetc : gzgetc; nextchar...
19
by: Ross A. Finlayson | last post by:
Hi, I hope you can help me understand the varargs facility. Say I am programming in ISO C including stdarg.h and I declare a function as so: void log_printf(const char* logfilename, const...
23
by: Timothy Madden | last post by:
Hello all. I program C++ since a lot of time now and I still don't know this simple thing: what's the problem with local functions so they are not part of C++ ? There surely are many people...
11
by: cps | last post by:
Hi, I'm a C programmer taking my first steps into the world of C++. I'm currently developing a C++ 3D graphics application using GLUT (OpenGL Utility Toolkit written in C) for the GUI...
15
by: Christian Christmann | last post by:
Hi, in which situations pointers to functions might be more efficient/convenient than a direct function call? In the example I've found so far, I see no advantage of using pointers to...
12
by: bgold | last post by:
Hey. I have a base class (SPRITE), and using this base class I have derived a large number of derived classes (PERSON, BULLET, MISSILE, etc.). Now, at a certain point in my program, I have a pair...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.