Hi everyone.

So here's the case, I am a newcomer in C++ and I have been working on a small begginers program ; it adds 2 numbers after adding 1 to each of those 2 numbers . Let's say if we put 4,5 then it will add 1 to both of them (5,6) and the result after adding them both will be 11 of course.

Here's the program that I have made for this using pointers:

#include <iostream.h>
int sum(int *a, int *b);
main()
{ int x,y,z;
x=12;
y=8;
z=sum (&x,&y);
cout<<"sum("<<x<<","<<y<<")="<<z;
return(0); }
int sum( int *a, int *b)
{ *a=*a+1;
*b = *b+1;
return (*a+*b); }

The first thing, I wanna make sure that this program is right and it's the way to do the request in pointers. I think it's the right way cuz I tested it and run it without getting any errors.

The second thing is that I wanna know how I can make this program work without getting the pointers to do the return method. I mean doing the request directly without needing to put the return line.

I think I should make a third pointer and also add it in the line:

z=sum(&x,&y);

But what else then should I do? Should I erase the last 3 code lines and add it to the main class?

Waiting for your anyone's help.

There is absolutely no need for your main() not to return 0 in this program. That's why, in C++, we also have user-defined functions. Why not perform the calculations in a function of your own,say increment_add(),then return the sum through the function:

#include<iostream>
using namespace std;
int increment_add(int x,int y){
int sum;//for the final answer
//perform your calculations here
return sum;
}
int main()
{
cout<<"enter the first variable here: ";
int x=0;y=0;
cin>>x;
cout<<endl<<"Enter the second variable here: ";
cin>>y;
cout<<endl;
int final=increment_add(x,y);
cout<<"The sum, after incrementing both variables ,is "<<final<<endl;
return 0;
}

This should be a better way.

There is absolutely no need for your main() not to return 0 in this program. That's why, in C++, we also have user-defined functions. Why not perform the calculations in a function of your own,say increment_add(),then return the sum through the function:

#include<iostream>
using namespace std;
int increment_add(int x,int y){
int sum;//for the final answer
//perform your calculations here
return sum;
}
int main()
{
cout<<"enter the first variable here: ";
int x=0;y=0;
cin>>x;
cout<<endl<<"Enter the second variable here: ";
cin>>y;
cout<<endl;
int final=increment_add(x,y);
cout<<"The sum, after incrementing both variables ,is "<<final<<endl;
return 0;
}

This should be a better way.

I can make the program with functions, but that's not what's gonna be asked from me in my test this week : D

I'm gonna be asked to design a simple program (I came up with that simple one I explained above) with pointers in two ways: with the return method, and without it.

I think my program with the return is working well, I didn't get any errors in it. But I need to know how to design it with pointers that doesn't need the return. The numbers got to be changed without the need of the return.

Change the function's return type to void and pass the third argument also as a pointer. (Though this first version that you have is more 'natural'/usable, i.e. it returns the calculated value.)

Change the function's return type to void and pass the third argument also as a pointer. (Though this first version that you have is more 'natural'/usable, i.e. it returns the calculated value.)

Here's what I've done as far as what I understood but I still have errros:

#include <iostream.h>
void sum( int *a, int *b, int *c);

main()
{
int x,y,z;
x=12;
y=8;
z=sum(&x,&y,&s);
cout<<endl<<"sum("<<x<<","<<y<<")="<<z;
return(0);
}
void sum( int *a, int *b, int *c);
{
*a=*a+1;
*b=*b+1;
}

How can I fix it?

Get rid of the z= on line 9, and pass in the address of z as the third parameter (just like you did with x and y).

Now, in your function you must actually do the calculation with *c (as changing a and b will do nothing to c).

Oh, jonsca was fast, so I reduce this reply to state just the following:
A void function does not return anything. So it is an error to try to assign the function's return value to anything because there is no return value.

So you are supposed to use the function as

// Call the function (with existing arguments)..
sum(&x,&y,&z);
// .. now it's done the calculation, see what you got
cout << "sum: " = z << endl;
#include <iostream.h>
void sum( int *a, int *b, int *c);

main()
{
int x,y,z;
x=12;
y=8;
sum(&x,&y,&z);
cout<<"sum:"<<z<<endl;
return(0);
}
void sum( int *a, int *b, int *c);
{
*a=*a+1;
*b=*b+1;
*c= *a + *b
}

Still got 1 error. What's left to correct?

Still got 1 error.

Tell us what the error is ...

[EDIT]
To be more clear; tell us the compiler error message, if you are receiving one.

Tell us what the error is ...

Error NONAME00.CPP 13: Declaration terminated incorrectly

By the way it should be #include <iostream> not iostream.h which is an outdated prestandard file. main() should be of int type (the way you have it, it's probably implied but that's not a good habit to get into).

Also, what if you wanted to use x and y further along on your program. There might be a problem there...

Error NONAME00.CPP 13: Declaration terminated incorrectly

A semicolon is missing *c= *a + *b; And you could also clearly state that main() returns an int . So rather make it ..

int main()

Check line 13 (and line 17 for good measure). One has something it doesn't need and the other needs something.

commented: line 13 == good catch +5

By the way it should be #include <iostream> not iostream.h which is an outdated prestandard file. main() should be of int type (the way you have it, it's probably implied but that's not a good habit to get into).

Also, what if you wanted to use x and y further along on your program. There might be a problem there...

Yeah I know, when I use Visual Studio I write

#include<iostream>

and I add the line:

using namespace std

. But I'm now using Turbo C++ which doesn't give me any problems while using the old

#include<iostream.h>

and I should use it like this cuz we kinda study it like this and I don't wanna mix things up.

I should use it like this cuz we kinda study it like this and I don't wanna mix things up

Okay, but just be aware that for all the rest of the programming world, we like to leave 1993 right where it is.

A semicolon is missing *c= *a + *b; And you could also clearly state that main() returns an int . So rather make it ..

int main()

Done that. Still the same error.

Check line 13 (and line 17 for good measure). One has something it doesn't need and the other needs something.

I think it goes something like this:

#include <iostream.h>
void sum( int *a, int *b, int *c);
int main()
{
int x,y,z;
x=12;
y=8;
sum(&x,&y,&z);
cout<<"sum:"<<z<<endl;
return(0);
}
void sum( int *a, int *b);
{
*a=*a+1;
*b=*b+1;
*c= *a + *b;
}

But I still have the same error.

Did you check line 13 like the compiler was telling you to? You only need a semicolon after a prototype but not in the function definition.

What I was also saying before is do you want to increment a and b within your function? Remember this will change the values outside as well.

I think it goes something like this:

#include <iostream.h>
void sum( int *a, int *b, int *c);
int main()
{
int x,y,z;
x=12;
y=8;
sum(&x,&y,&z);
cout<<"sum:"<<z<<endl;
return(0);
}
void sum( int *a, int *b);
{
*a=*a+1;
*b=*b+1;
*c= *a + *b;
}

But I still have the same error.

There is one extraneous semicolon on the offending line ;)

Okay, but just be aware that for all the rest of the programming world, we like to leave 1993 right where it is.

I have a weird teacher : (

Last time he saw me writing the program in Visual Studio with <iostream> and "using namespace std" instead of <iostream.h> he told me to use Turbo C++ to follow up the leasons just like he gives it to us. The old 93 way I guess.

You're not the only one (by a long shot) that has that situation. I know you can't do anything about it, I was just giving you a hard time.

I have a weird teacher : (

Last time he saw me writing the program in Visual Studio with <iostream> and "using namespace std" instead of <iostream.h> he told me to use Turbo C++ to follow up the leasons just like he gives it to us. The old 93 way I guess.

Well, your teacher is bound to his rules and regulations and other things alike, I would assume.

There is one extraneous semicolon on the offending line ;)

What's an offending line ? :?:

What's an offending line ? :?:

The source code line that gives you trouble.

You're not the only one (by a long shot) that has that situation. I know you can't do anything about it, I was just giving you a hard time.

Nah it's ok. I guess I'll just have to play by his rules, and all I care about is knowing how to finish this program and practicing it good in other examples of my own.

Well, your teacher is bound to his rules and regulations and other things alike, I would assume.

Yeah the grumpy kind of teacher, you know.

Yeah the grumpy kind of teacher, you know.

I meant to say, that this problem is in the educational system/resources, so your teacher is probably quite restricted by that.
If he's grumpy, that's another thing you have to live with.

The source code line that gives you trouble.

You mean the highlighted line that the program highlights it as an error? Well, it's line no. 13, and I don't think that it needs a semicolon.

I meant to say, that this problem is in the educational system/resources, so your teacher is probably quite restricted by that.
If he's grumpy, that's another thing you have to live with.

No it's not. The system in our education gives the teacher the free will to do whatever he may see as good to go, so I think it was his choice to work with this old resources and not give us the up-to-date programming methods in C++.

You mean the highlighted line that the program highlights it as an error? Well, it's line no. 13, and I don't think that it needs a semicolon.

Yes you are right, in general depending on the compiler and the error, sometimes the error message points you straight to the line that needs fixing, sometimes not. If not, then there are error(s) preceding the offending line.

The problem that there is, is that the function definition ends with a semicolon and that is plain wrong. Function declarations/prototypes need to end with a semicolon but definitions not. So, just remove the extra semicolon there (in this case line #12, I think).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.