|
-
Sep 23rd, 2001, 08:09 PM
#1
My first C++ program need help please
I want the user to choose 5 options like you can see in the switch below in my code, but I have 39 errors and I can not see one of them!
Well i post the code here, i will continue to work on it but if you can show me my error that will give me a lot for my knowledge.
Thx You in advance!!!!!!!!
Code:
#include <iostream.h>
#include <conio.h>
int x;
int y;
int addit(int x, int y);
int soust(int x, int y);
int multi(int x, int y);
int divide(int x, int y);
int exit(int x, int y);
int main()
{
int input;
cout<<"1. "Add function"<<endl;
cout<<"2. "Soustract function"<<endl;
cout<<"3. "Multi function"<<endl;
cout<<"4. "Divide function"<<endl;
cout<<"5. "Exit"<<endl;
cin>>input;
cout<<"First number"<<endl;
cin>>x;
cout<<"Second number"<<endl;
cin>>y;
switch (input)
{
case 1: addit();
addit(x,y);
cout<<addit(x,y);
break;
case 2: soust();
soust(x,y);
cout<<soust(x,y);
break;
case 3: multi();
mutli(x,y);
break;
case 4: divide();
divide(x,y);
cout<<divide(x,y);
break;
case 5: Exit();
return 0;
break;
default:
cout<<"Error, bad input, quitting");
}
return 0;
}
int addit(int x,int y)
{
return x+y;
}
int soust(int x,int y)
{
return x-y;
}
int multi(int x,int y)
{
return x*y;
}
int divide(int x,int y)
{
return x/y;
}
-
Sep 23rd, 2001, 08:13 PM
#2
Fanatic Member
Re: My first C++ program need help please
You don't have to call the functions.
will be enough. You are also getting errors because you are calling the functions with no parameters. You need addit(x,y);, you can't have just addit(). Also, you put mutli instead of multi in case 3.
Get rid of the parentheses after the cout in the default case, too.
In case 5, you have exit() but your function is defined as Exit(). Remember that C++ is case-sensitive.
You don't need the break; in case 5 because the program will be done already.
Why do you have conio.h included? As far as I can see, you don't need it.
Be careful, since divide() returns an int if you do divide(3,4) you will get 0, not 0.75, because it drops off the zero.
Last edited by Wynd; Sep 23rd, 2001 at 08:21 PM.
Alcohol & calculus don't mix.
Never drink & derive.
-
Sep 24th, 2001, 01:46 PM
#3
Than you wynd but I changed all what you said and I have other error than I will paste you after the code:
This is my current code after correcting most of your suggestion.
Code:
#include <iostream.h>
int x;
int y;
int main()
{
int input;
cout<<"1. "Add function"<<endl;
cout<<"2. "Soustract function"<<endl;
cout<<"3. "Multi function"<<endl;
cout<<"4. "Divide function"<<endl;
cout<<"5. "Exit"<<endl;
cin>>input;
cout<<"First number"<<endl;
cin>>x;
cout<<"Second number"<<endl;
cin>>y;
switch (input)
{
case 1: addit(x,y);
cout<<addit(x,y);
break;
case 2: soust(x,y);
cout<<soust(x,y);
break;
case 3: multi(x,y);
cout<<mutli(x,y);
break;
case 4: divide(x,y);
divide(x,y);
cout<<divide(x,y);
break;
case 5: exit();
return 0;
default:
cout<<"Error, bad input, quitting");
}
return 0;
}
int addit(int x,int y)
{
return x+y;
}
int soust(int x,int y)
{
return x-y;
}
int multi(int x,int y)
{
return x*y;
}
int divide(int x,int y)
{
return x/y;
}
Error :
--------------------Configuration: mathematic - Win32 Debug--------------------
Compiling...
mathematic.cpp
C:\cplusplus\mathematic\mathematic.cpp(11) : error C2146: syntax error : missing ';' before identifier 'Add'
C:\cplusplus\mathematic\mathematic.cpp(11) : error C2001: newline in constant
C:\cplusplus\mathematic\mathematic.cpp(11) : error C2065: 'Add' : undeclared identifier
C:\cplusplus\mathematic\mathematic.cpp(11) : error C2146: syntax error : missing ';' before identifier 'function'
C:\cplusplus\mathematic\mathematic.cpp(11) : error C2065: 'function' : undeclared identifier
C:\cplusplus\mathematic\mathematic.cpp(11) : error C2143: syntax error : missing ';' before 'string'
C:\cplusplus\mathematic\mathematic.cpp(12) : error C2146: syntax error : missing ';' before identifier 'cout'
C:\cplusplus\mathematic\mathematic.cpp(12) : error C2146: syntax error : missing ';' before identifier 'Soustract'
C:\cplusplus\mathematic\mathematic.cpp(12) : error C2001: newline in constant
C:\cplusplus\mathematic\mathematic.cpp(12) : error C2065: 'Soustract' : undeclared identifier
C:\cplusplus\mathematic\mathematic.cpp(12) : error C2146: syntax error : missing ';' before identifier 'function'
C:\cplusplus\mathematic\mathematic.cpp(12) : error C2143: syntax error : missing ';' before 'string'
C:\cplusplus\mathematic\mathematic.cpp(13) : error C2146: syntax error : missing ';' before identifier 'cout'
C:\cplusplus\mathematic\mathematic.cpp(13) : error C2146: syntax error : missing ';' before identifier 'Multi'
C:\cplusplus\mathematic\mathematic.cpp(13) : error C2001: newline in constant
C:\cplusplus\mathematic\mathematic.cpp(13) : error C2065: 'Multi' : undeclared identifier
C:\cplusplus\mathematic\mathematic.cpp(13) : error C2146: syntax error : missing ';' before identifier 'function'
C:\cplusplus\mathematic\mathematic.cpp(13) : error C2143: syntax error : missing ';' before 'string'
C:\cplusplus\mathematic\mathematic.cpp(14) : error C2146: syntax error : missing ';' before identifier 'cout'
C:\cplusplus\mathematic\mathematic.cpp(14) : error C2146: syntax error : missing ';' before identifier 'Divide'
C:\cplusplus\mathematic\mathematic.cpp(14) : error C2001: newline in constant
C:\cplusplus\mathematic\mathematic.cpp(14) : error C2065: 'Divide' : undeclared identifier
C:\cplusplus\mathematic\mathematic.cpp(14) : error C2146: syntax error : missing ';' before identifier 'function'
C:\cplusplus\mathematic\mathematic.cpp(14) : error C2143: syntax error : missing ';' before 'string'
C:\cplusplus\mathematic\mathematic.cpp(15) : error C2146: syntax error : missing ';' before identifier 'cout'
C:\cplusplus\mathematic\mathematic.cpp(15) : error C2001: newline in constant
C:\cplusplus\mathematic\mathematic.cpp(15) : error C2146: syntax error : missing ';' before identifier 'Exit'
C:\cplusplus\mathematic\mathematic.cpp(15) : error C2065: 'Exit' : undeclared identifier
C:\cplusplus\mathematic\mathematic.cpp(15) : error C2143: syntax error : missing ';' before 'string'
C:\cplusplus\mathematic\mathematic.cpp(16) : error C2146: syntax error : missing ';' before identifier 'cin'
C:\cplusplus\mathematic\mathematic.cpp(24) : error C2065: 'addit' : undeclared identifier
C:\cplusplus\mathematic\mathematic.cpp(27) : error C2065: 'soust' : undeclared identifier
C:\cplusplus\mathematic\mathematic.cpp(30) : error C2065: 'multi' : undeclared identifier
C:\cplusplus\mathematic\mathematic.cpp(31) : error C2065: 'mutli' : undeclared identifier
C:\cplusplus\mathematic\mathematic.cpp(33) : error C2065: 'divide' : undeclared identifier
C:\cplusplus\mathematic\mathematic.cpp(37) : error C2065: 'exit' : undeclared identifier
C:\cplusplus\mathematic\mathematic.cpp(40) : error C2059: syntax error : ')'
C:\cplusplus\mathematic\mathematic.cpp(46) : error C2373: 'addit' : redefinition; different type modifiers
C:\cplusplus\mathematic\mathematic.cpp(50) : error C2373: 'soust' : redefinition; different type modifiers
C:\cplusplus\mathematic\mathematic.cpp(54) : error C2373: 'multi' : redefinition; different type modifiers
C:\cplusplus\mathematic\mathematic.cpp(58) : error C2373: 'divide' : redefinition; different type modifiers
Error executing cl.exe.
mathematic.exe - 41 error(s), 0 warning(s)
-
Sep 24th, 2001, 02:16 PM
#4
Addicted Member
You didn't declare your functions & a few too many quotation marks in your cout statements.
Code:
#include <iostream.h>
int addit(int x,int y);
int soust(int x,int y);
int multi(int x,int y);
int divide(int x,int y);
int x;
int y;
int main()
{
int input;
cout<<"1. Add function"<<endl;
cout<<"2. Soustract function"<<endl;
cout<<"3. Multi function"<<endl;
cout<<"4. Divide function"<<endl;
cout<<"5. Exit"<<endl;
cin>>input;
cout<<"First number"<<endl;
cin>>x;
cout<<"Second number"<<endl;
cin>>y;
switch (input)
{
case 1: addit(x,y);
cout<<addit(x,y);
break;
case 2: soust(x,y);
cout<<soust(x,y);
break;
case 3: multi(x,y);
cout<<mutli(x,y);
break;
case 4: divide(x,y);
divide(x,y);
cout<<divide(x,y);
break;
case 5: exit();
return 0;
default:
cout<<"Error, bad input, quitting");
}
return 0;
}
int addit(int x,int y)
{
return x+y;
}
int soust(int x,int y)
{
return x-y;
}
int multi(int x,int y)
{
return x*y;
}
int divide(int x,int y)
{
return x/y;
}
-
Sep 24th, 2001, 03:45 PM
#5
--------------------Configuration: mathematic - Win32 Debug--------------------
Compiling...
mathematic.cpp
C:\cplusplus\mathematic\mathematic.cpp(41) : error C2065: 'exit' : undeclared identifier
Error executing cl.exe.
mathematic.exe - 1 error(s), 0 warning(s)
Can you tell me if they have a way to know what line I am I see than the error is 41 but I dont see the number in the left side of C++ 
This my code :
Code:
#include <iostream.h>
int addit(int x,int y);
int soust(int x,int y);
int multi(int x,int y);
int divide(int x,int y);
int x;
int y;
int main()
{
int input;
cout<<"1. Add function"<<endl;
cout<<"2. Soustract function"<<endl;
cout<<"3. Multi function"<<endl;
cout<<"4. Divide function"<<endl;
cout<<"5. Exit"<<endl;
cin>>input;
cout<<"First number"<<endl;
cin>>x;
cout<<"Second number"<<endl;
cin>>y;
switch (input)
{
case 1: addit(x,y);
cout<<addit(x,y);
break;
case 2: soust(x,y);
cout<<soust(x,y);
break;
case 3: multi(x,y);
cout<<multi(x,y);
break;
case 4: divide(x,y);
divide(x,y);
cout<<divide(x,y);
break;
case 5: exit();
return 0;
default:
cout<<"Error, bad input, quitting";
}
return 0;
}
int addit(int x,int y)
{
return x+y;
}
int soust(int x,int y)
{
return x-y;
}
int multi(int x,int y)
{
return x*y;
}
int divide(int x,int y)
{
return x/y;
}
-
Sep 24th, 2001, 04:13 PM
#6
My exit function do not work...I know in vb I have to do End or Unload Me but how I do that in C++ ?
-
Sep 24th, 2001, 05:12 PM
#7
This is so a basic question guy.
1- How can I do my function to press 5 to leave ?
-
Sep 24th, 2001, 05:17 PM
#8
I have now 2 questions :
1-How can I do my function exit?
2-How can I make something like that :
I want to said : if x = nothing then write 0 but "" did not work.
-
Sep 24th, 2001, 05:21 PM
#9
Frenzied Member
1. To terminate the app use
2.
You can't do this like "this"
What kind of var is x . First you treat it like a string and then like an integer. If it is a char variable use strcmp to compare it and see if it equals some value. If it is an integer do something like this
Code:
if (x == 5 )
return 0;
-
Sep 24th, 2001, 05:53 PM
#10
Fanatic Member
Oh for crying out loud, double click on the error to go to that line #. You dont even need to know about the line # because at the status (bottom right), it will show you the line # and the column# also if you pressed insert key or not.
Also, Every C++ Function needs to be defined beforehand... that means that you need a function prototype BEFORE you actually call it, unless you define the function before main.
Code for function prototype:
Code:
returntype functionname(param1type param1label, param2type param2label [, ...]);
Hint, take out a 8.5x11 sheet of standard paper and write BIG BOLD PRINT A SEMI COLON ( ; ) NEVER FORGET THE SEMI COLON.
Also, C++ is CASE-sensitive!!!!!!!!!!! VERY Case sENSitiVE!!!
-
Sep 24th, 2001, 06:04 PM
#11
Fanatic Member
Code:
case 1: addit(x,y);
cout<<addit(x,y);
break;
case 2: soust(x,y);
cout<<soust(x,y);
break;
case 3: multi(x,y);
cout<<multi(x,y);
break;
case 4: divide(x,y);
divide(x,y);
cout<<divide(x,y);
break;
You don't need to call the function more than once, like I said before. Just do this:
Code:
case 1:
cout<<addit(x,y);
break;
case 2:
cout<<soust(x,y);
break;
case 3:
cout<<multi(x,y);
break;
case 4:
cout<<divide(x,y);
break;
case 5:
return 0;
default:
cout<<"Error, bad input, quitting";
This is because the function returns a value. So if you have cout<<addit(5,3); it's the same as saying cout<<8;.
Last edited by Wynd; Sep 24th, 2001 at 06:07 PM.
Alcohol & calculus don't mix.
Never drink & derive.
-
Sep 24th, 2001, 07:25 PM
#12
Big thank you to everybody who helped me and UNDERSTAND me than we have to start a day 
All work but the Case 5: return 0; don't work but it's ok 
Thx you
-
Sep 24th, 2001, 09:08 PM
#13
Lively Member
actually those should be floats, that way you get a real number, because if you divide 2/3, you dont want the answer 0.
-
Sep 25th, 2001, 05:34 AM
#14
I need my code I used Double....should I change it for Float ? In Vb we dont have float so I dont know that type
-
Sep 25th, 2001, 06:20 AM
#15
Frenzied Member
It depends of the values the var is going to recieve. Here is the difference between double and float
Code:
float 4 bytes 3.4E +/- 38 (7 digits)
double 8 bytes 1.7E +/- 308 (15 digits)
-
Sep 25th, 2001, 11:21 AM
#16
Monday Morning Lunatic
Technically long double (a standard type) should be 80 bits (10 bytes), but it defaults to 64-bit on Windows.
Although my work SGI has quad-precision floats (sizeof(long double) == 128 )
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Sep 25th, 2001, 06:51 PM
#17
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|