Menu

Very simple program please help a new guy!

intint50
2010-02-18
2012-09-26
  • intint50

    intint50 - 2010-02-18

    Hello! I am a new member to SourceForge and new to C++ also, I am using
    Dev-C++ 4.9.9.2 with a windows vista home premium, and trying to compile a
    simple program:

    include <iostream>

    using namespace std;

    int first_number;

    int second_number;

    int result;

    int main();
    {
    char operator;

    cout << "Hello, and welcome to my calculator. Please enter the first number:";
    cin >> first_number;

    cout << "Now, please enter the operator:";
    cin >> operator;

    cout << "Now, please enter your second number:";
    cin >> second_number;

    if(operator == "") result == first_numbersecond_number;
    else if(operator == "/") result == first_number/second_number;
    else if(operator == "+") result == first_number+second_number;
    else if(operator == "-") result == first_number-second_number;

    return 0;

    }

    When I try to compile, I get the compile log:

    include <iostream>

    using namespace std;

    int first_number;

    int second_number;

    int result;

    int main();
    {
    char operator;

    cout << "Hello, and welcome to my calculator. Please enter the first number:";
    cin >> first_number;

    cout << "Now, please enter the operator:";
    cin >> operator;

    cout << "Now, please enter your second number:";
    cin >> second_number;

    if(operator == "") result == first_numbersecond_number;
    else if(operator == "/") result == first_number/second_number;
    else if(operator == "+") result == first_number+second_number;
    else if(operator == "-") result == first_number-second_number;

    return 0;

    }

    I think it might be something to do with the semicolon after int main() but
    when i remove that i get the compile log:

    include <iostream>

    using namespace std;

    int first_number;

    int second_number;

    int result;

    int main();
    {
    char operator;

    cout << "Hello, and welcome to my calculator. Please enter the first number:";
    cin >> first_number;

    cout << "Now, please enter the operator:";
    cin >> operator;

    cout << "Now, please enter your second number:";
    cin >> second_number;

    if(operator == "") result == first_numbersecond_number;
    else if(operator == "/") result == first_number/second_number;
    else if(operator == "+") result == first_number+second_number;
    else if(operator == "-") result == first_number-second_number;

    return 0;

    }

    Please help that would be great :)

    intint50

     
  • intint50

    intint50 - 2010-02-18

    Whoops sorry let me rephrase that: when I try to compile the original code i
    get:

    Compiler: Default compiler
    Executing g++.exe...
    g++.exe "C:\Users\User\Desktop\calc.cpp" -o "C:\Users\User\Desktop\calc.exe"
    -I"C:\Dev-Cpp\lib\gcc\mingw32\3.4.2\include" -I"C:\Dev-
    Cpp\include\c++\3.4.2\backward" -I"C:\Dev-Cpp\include\c++\3.4.2\mingw32" -I"C
    :\Dev-Cpp\include\c++\3.4.2" -I"C:\Dev-Cpp\include"
    C:\Users\User\Desktop\calc.cpp:11: error: expected unqualified-id before '{'
    token
    C:\Users\User\Desktop\calc.cpp:11: error: expected ,' or;' before '{' token

    Execution terminated

    Then when I try to compile the second one I get:

    Compiler: Default compiler
    Executing g++.exe...
    g++.exe "C:\Users\User\Desktop\calc.cpp" -o "C:\Users\User\Desktop\calc.exe"
    -I"C:\Dev-Cpp\lib\gcc\mingw32\3.4.2\include" -I"C:\Dev-
    Cpp\include\c++\3.4.2\backward" -I"C:\Dev-Cpp\include\c++\3.4.2\mingw32" -I"C
    :\Dev-Cpp\include\c++\3.4.2" -I"C:\Dev-Cpp\include"
    C:\Users\User\Desktop\calc.cpp: In function `int main()':
    C:\Users\User\Desktop\calc.cpp:14: error: expected primary-expression before
    "char"

    C:\Users\User\Desktop\calc.cpp:14: error: expected `;' before "char"

    C:\Users\User\Desktop\calc.cpp:20: error: expected identifier before ';' token

    C:\Users\User\Desktop\calc.cpp:25: error: expected )' before string constant C:\Users\User\Desktop\calc.cpp:25: error: could not convertstd::operator=='
    to `bool'

    C:\Users\User\Desktop\calc.cpp:26: error: expected `)' before string constant

    C:\Users\User\Desktop\calc.cpp:26: error: could not convert std::operator==' tobool'
    C:\Users\User\Desktop\calc.cpp:27: error: expected )' before string constant C:\Users\User\Desktop\calc.cpp:27: error: could not convertstd::operator=='
    to bool' C:\Users\User\Desktop\calc.cpp:28: error: expected)' before string constant

    C:\Users\User\Desktop\calc.cpp:28: error: could not convert std::operator==' tobool'

    Execution terminated

    Sorry for my mistake!

     
  • Patrice Denis

    Patrice Denis - 2010-02-18

    I guest you just have to remove the semicolon after "int main()" line 11

     
  • Patrice Denis

    Patrice Denis - 2010-02-18

    You need to learn how to write a program in C++. These are not DevCpp errors
    but only C++ syntax errors.
    You have done several mistakes in your code, a solution could be :

    include <iostream>

    using namespace std;

    int main()
    {

    int first_number;

    int second_number;

    int result;

    char ch_operator;

    cout << "Hello, and welcome to my calculator. Please enter the first number:";
    cin >> first_number;

    cout << "Now, please enter the operator:";
    cin >> ch_operator;

    cout << "Now, please enter your second number:";
    cin >> second_number;

    if(ch_operator == '') result = first_numbersecond_number;
    else if(ch_operator == '/') result = first_number/second_number;
    else if(ch_operator == '+') result = first_number+second_number;
    else if(ch_operator == '-') result = first_number-second_number;

    cout << "Result =" << result;
    return 0;

    }

    the name "operator" is a reserved name for C++ programming,
    to compare characters you need to use 'c' instead of "c" because it would
    compare strings
    and a basic affectation is done with "=" not "==" which is reserved for tests

     
  • intint50

    intint50 - 2010-02-18

    Thanks all for your comments, now I am trying to complicate things slightly by
    adding a switch statement and a for loop. My code:

    include <iostream>

    using namespace std;

    double first_number, second_number, result;
    char oper;

    int main()

    {

    cout << "Welcome to my calculator. Please enter the first number: ";
    cin >> first_number;

    cout << "Now please enter the operator(*, /, +, -): ";
    cin >> oper;

    cout << "Now please enter the second number: ";
    cin >> second_number;

    cout << "The answer is: ";

    switch(oper)
    {
    case '': result = first_numbersecond_number;
    break;
    case '/': result = first_number/second_number;
    break;
    case '+': result = first_number+second_number;
    break;
    case '-': result = first_number-second_number;
    break;
    default: cout << "Sorry, invalid operator";

    }
    cout << result;

    char Yes_No;
    cout << "\n \n Would you like to continue(Yes/No)?";
    if(Yes_No == 'Y'){
    char Y_N;
    int counter;

    for(Y_N == 'Y'; Y_N == 'Y'; result +=0) {
    cout << "Please enter the operator: ";
    cin >> oper;

    cout << "Please enter another number: ";
    cin >> second_number;

    cout << "The answer is: ";

    switch(oper){

    case '': result = resultsecond_number;
    break;
    case '/': result = result/second_number;
    break;
    case '+': result = result+second_number;
    break;
    case '-': result = result-second_number;
    break;
    default: cout << "Sorry, invalid operator";
    }

    cout << "Would you like to continue? ";
    cin >> Y_N;
    }
    else{
    cout << "Thank you for using my calculator. ";
    }

    }

    return 0;
    }

    My comiple log:

    Compiler: Default compiler
    Executing g++.exe...
    g++.exe "C:\Users\User\Desktop\calc.cpp" -o "C:\Users\User\Desktop\calc.exe"
    -I"C:\Dev-Cpp\lib\gcc\mingw32\3.4.2\include" -I"C:\Dev-
    Cpp\include\c++\3.4.2\backward" -I"C:\Dev-Cpp\include\c++\3.4.2\mingw32" -I"C
    :\Dev-Cpp\include\c++\3.4.2" -I"C:\Dev-Cpp\include"
    C:\Users\User\Desktop\calc.cpp: In function `int main()':
    C:\Users\User\Desktop\calc.cpp:69: error: expected primary-expression before
    "else"

    C:\Users\User\Desktop\calc.cpp:69: error: expected `;' before "else"

    Execution terminated

    Help would be appreciated as it was before.

     
  • cpns

    cpns - 2010-02-18

    Use the code mark-up when posting code and logs not quite mark-up.

    I wonder how the code looked before the indentation was removed? If you used a
    sensible indentation style it would be clear to you that the else clause at
    line 69 is after the closing brace of the for loop, so is not associated with
    the if.

    That is not the only error:

            for(Y_N == 'Y'; Y_N == 'Y'; result +=0)
    

    should read

            for(Y_N = 'Y'; Y_N == 'Y'; result +=0)
    

    but even then adding zero to result is pointless; why not just

            for(Y_N = 'Y'; Y_N == 'Y'; )
    

    or better;

        Y_N = 'Y' ;
        while( Y_N == 'Y' )
    

    ?

    The variable counter is unused - was that intended to be there for something?

    Yes_No is unitialised and not assigned before it is tested. You prompt the
    user but do not accept any input.

    The following compiles without warnings or errors (in VC++ with /W4). That is
    not to say that it is correct or even good quality code (the repeated block of
    code could trivially be avoided); I have not executed it or tried to fathom
    your intent:

    #include <iostream>
    using namespace std;
    
    double first_number, second_number, result;
    char oper;
    
    int main()
    
    {
    
        cout << "Welcome to my calculator. Please enter the first number: ";
        cin >> first_number;
    
        cout << "Now please enter the operator(*, /, +, -): ";
        cin >> oper;
    
        cout << "Now please enter the second number: ";
        cin >> second_number;
    
        cout << "The answer is: ";
    
        switch(oper)
        {
            case '*': result = first_number*second_number;
                break;
            case '/': result = first_number/second_number;
                break;
            case '+': result = first_number+second_number;
                break;
            case '-': result = first_number-second_number;
                break;
            default: cout << "Sorry, invalid operator";
        }
    
        cout << result;
    
        char Yes_No = 'Y' ;
        cout << "\n \n Would you like to continue(Yes/No)?";
        if(Yes_No == 'Y')
        {
            char Y_N;
    
            for(Y_N = 'Y'; Y_N == 'Y'; result +=0) 
            {
                cout << "Please enter the operator: ";
                cin >> oper;
    
                cout << "Please enter another number: ";
                cin >> second_number;
    
                cout << "The answer is: ";
    
                switch(oper)
                {
                    case '*': result = result*second_number;
                        break;
                    case '/': result = result/second_number;
                        break;
                    case '+': result = result+second_number;
                        break;
                    case '-': result = result-second_number;
                        break;
                    default: cout << "Sorry, invalid operator";
                }
    
                cout << "Would you like to continue? ";
                cin >> Y_N;
            }
        }
        else
        {
            cout << "Thank you for using my calculator. ";
        }
    
        return 0;
    }
    
     
  • intint50

    intint50 - 2010-02-18

    Thanks :)

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.