cs2144 and 2146

Im not to sure whats going on in my code.
granted ,I am a noob.
What I'm trying to do is create a function and then call that function in the mai.n
I think I have this covered but I just cant get past these syntax errors, which is very frustrating since I think I should be able to spot that.
I built the function from the template provided here at cplusplus.
Also this is my first program outside of hello worldso my experience is very lacking.
ok so heres the code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;

int multiplication(int a ,int b)
 {
	 int r;
	 r=a*b;
	 return(r);
 }
int main()
	 {
		 int y int x;
			 cout<<"enter a number: ";
		     cin>>y;
				 x = multiplication (y,2);
				 cout<<"the result is: "x;
			 return 0;
	 }


Also I'm not really trying to be told what to do , just would like to be pointed in the right direction so I can see what I'm missing
thanks to all ahead of time

Last edited on
line 12: it's not a valid syntax. You can declare variables like this
int x,y;
or
1
2
int x;
int y;
ok cool I did fix this and it works . I do understand where i went wrong now.
There is another issue now.
It seems when I compile and debug (visual c++ express)
it opens the console and ask for the input
I enter the input
the console blinks the output (line 16) and then the console disappears
I read in another forum about about a system pause command but this seems kind off excessive
am I missing something like <<endl or something ?
You need <<
 
cout <<"the result is: "<<x;



To see output of console you need
system("pause");
or
cin.get();
before
return 0;
Just a tips, if you dont want to much rows in your code you can do this:

int multiplication(int a ,int b)
 {
	 return a*b;
 }


instead of:


int multiplication(int a ,int b)
 {
	 int r;
	 r=a*b;
	 return(r);
 }

well system(" pause") does indeed work
not sure if im getting the syntax if cin.get() this did nothing as typed.
dude use Ctrl + F5... then you would not need to code system("PAUSE")


.
.
.

.
do tell me iF it helps ..
System calls are problematic and should be avoided: http://cplusplus.com/forum/beginner/1988/

If you're using MS Visual Express why not just use the 'Start without debugging' option (cntl-F5).
It will hold the console window open for you automatically.
I want to thank all that replied to this thread.All of the reply were very helpful and did make a difference in the final code.Also Ctrl+f5 was exactly what I was looking for. in case anyones interested in the final product
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream> //headers
using namespace std;
double doubleit(double a ,double b) //function for multiplying usr input * 2
 {
	 return a*b;           //body of function
 }
double funct2(double c, double d, double e) //function for adding and dividing the output from function doubelit
{
	return (c+d)/e; // body of function 
}
void main ()
	 {
		 double number; double x; double z; //variable declaration 
			 cout<<"Enter a number: "; // asks usr for input			 
		     cin>>number; //usr input is variable number

		     x = doubleit (number,2); //funtion call to doubleit

             cout <<""<<endl; //skips a line in display
             cout <<"The number Doubled is: "<<x<<endl; //output of doubleit to usr as x

			 z=funct2(x,7,2); //function call to funct2

             cout <<""<<endl; // skips a line in 
		     cout <<"The (Doubled number + 7) / 2 is : "<<z<<endl; // output of funct2 to usr as z
             cout <<""<<endl; // skips a line in display			
             return; //return to main end of program

	 }  


Last edited on
Topic archived. No new replies allowed.