Results 1 to 17 of 17

Thread: My first C++ program need help please

  1. #1
    DaoK
    Guest

    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;
    }

  2. #2
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772

    Re: My first C++ program need help please

    You don't have to call the functions.
    Code:
    cout<<soust(x,y);
    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.

  3. #3
    DaoK
    Guest
    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)

  4. #4
    Addicted Member
    Join Date
    Mar 2001
    Posts
    157
    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;
    }

  5. #5
    DaoK
    Guest
    --------------------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;
    }

  6. #6
    DaoK
    Guest
    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++ ?

  7. #7
    DaoK
    Guest
    This is so a basic question guy.
    1- How can I do my function to press 5 to leave ?

  8. #8
    DaoK
    Guest
    I have now 2 questions :

    1-How can I do my function exit?

    2-How can I make something like that :
    Code:
          if (x == "" )
                     x=0;
    I want to said : if x = nothing then write 0 but "" did not work.

  9. #9
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    1. To terminate the app use
    Code:
    return 0;
    2.
    Code:
    if (x == "" )
                     x=0;

    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;
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  10. #10
    Fanatic Member MoMad's Avatar
    Join Date
    Oct 2000
    Location
    Seattle, WA
    Posts
    625
    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!!!
    :MoMad:
    Nice Sig!

    http://go.to/momad/ Status: Not Ready

  11. #11
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772
    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.

  12. #12
    DaoK
    Guest
    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

  13. #13
    Lively Member
    Join Date
    Jul 2001
    Posts
    67

    Talking

    actually those should be floats, that way you get a real number, because if you divide 2/3, you dont want the answer 0.

  14. #14
    DaoK
    Guest
    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

  15. #15
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    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)
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  16. #16
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  17. #17
    DaoK
    Guest
    Thx you all

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width