1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

First time programming ever and my first project has 1-21 errors please help in c++

  1. Sep 5, 2010 #1
    1. The problem statement, all variables and given/known data



    #include <iostream>
    using namespace std;

    int main()
    {

    int firstNo;// first number is stored here
    int secondNo,
    int resultAdd,// variable for result when first number and second number are added
    int resultSub,// variable for result when first number and second number are subtracted
    int resultMult,// variable for result when first number and second number are multiplied
    int resultDivi// variable for the reult when the first number and second number are divided
    ;

    // asks the user to choose the first number
    cout<< "Enter in the first Number -> /n";
    cin >> "firstNo";

    // asks the user for the second number
    cout<< "Enter in the second Number -> ";
    cin >> "secondNo";

    cout<< "which operation would you like to choose?";

    // result of the operation when addition is chosen
    resultAdd == firstNo + secondNo;
    cout<< "the sum of the numbers are ->/n";
    cin >> "resultAdd";


    // result of the operation when subtraction is chosen
    resultSub = firstNo - secondNo;
    cout<< "the difference of these numbers are ->/n";
    cin >> "resultSub";

    // result of the operation when multiplication is chosen
    resultMult == firstNo * secondNo;
    cout<< "the product of these numbers are ->/n";
    cin >> "resultMult";

    // result of the operation when division is chosen
    resultDivi == firstNo/seconNo;
    cout<< "the quotient of these two numbers are ->/n";
    cin >> "resultDivi"


    ;if (secondNo == 0 )// restriction put upon operation so that denominator cannot be 0
    firstNo/secondNo !==0;
    cout<< "the quotient of these two numbers are undefined :(";


    cout<<"/n";

    return 0;

    }

    i don't know what i did wrong, but i guess i did alot wrong, i just basically followed the template that my professor showed us, but the program he made just had one operation and that was addition.

    it went like this:
    Result = FirstNo + SecondNo;

    cout<< "these numbers add up to ->";
    cout<< Result;

    so i figured i could just make more than one variable and define them with operations.
     
    Last edited: Sep 5, 2010
  2. jcsd
  3. Sep 5, 2010 #2
    Re: first time programming ever and my first project has 1-21 errors please help in c

    error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'const char [8]' (or there is no acceptable conversion)
    error C2059: syntax error : '='
    error C2062: type 'int' unexpected

    are some error statements
     
    Last edited: Sep 5, 2010
  4. Sep 5, 2010 #3

    Mark44

    Staff: Mentor

    Re: first time programming ever and my first project has 1-21 errors please help in c

    The first declaration above is fine, but each of the next four has an error. To the compiler, these look like separate declarations, because each line starts with int, but don't end with a semicolon. In C and C++ and other C-like languages, all statements end with semicolons.

    You can declare secondNo, resultAdd, resultSub, resultMult, and resultDivi in one declaration like this
    int secondNo, resultAdd, resultSub, resultMult, and resultDivi;

    Or you can declare them in separation declarations like this
    int secondNo;
    int resultAdd;
    int resultSub;
    int resultMult;
    int resultDivi;

    You need a variable, not a string literal. Presumably this is why you declared firstNo. Similar comment on the next two groups of statements.
    The statement above is doing something, but not what you intended. It adds firstNo and secondNo, and determines whether this value is equal to the value in resultAdd. The result will be a binary value that will be discarded since it's not being saved to a variable.

    Use the assignment operator.
    There are two things wrong above. One, you should be using a variable with cin. Two, you should not ask for the sum of the numbers as input from the user. Your program already calculated it. Instead of doing input, your program should output the calculated result. This latter error is not a compiler (or syntax) error - it is a semantic error.
    Similar comment as before. The program should not be doing input; it should be displaying the calculated result. Similar comment in the next two blocks of code.
    What do you think this is doing?
     
  5. Sep 5, 2010 #4
    Re: first time programming ever and my first project has 1-21 errors please help in c

    i got it to work, but it is not doing what i want it to do. it works fine when i ask the user for two numbers it goes like this:
    Enter the first number->45
    Enter the second number->5
    what operator would you like to use? the sum of the number is 50/n->
    after you plug in any other operator sign it will output all the remaining operations:

    here is my new code:

    #include <iostream>
    using namespace std;

    int main()
    {

    int firstNo;// first number is stored here
    int secondNo;
    int resultAdd;//variable for result when first and second number are added
    int resultSub;//variable for result when first and second number are subtracted
    int resultMult;//variable for result when first and second number are multiplied
    int resultDivi//variable for result when first and second number are divided
    ;

    // asks the user to choose the first number
    cout<< "Enter in the first Number ->";
    cin >> firstNo;

    // asks the user for the second number
    cout<< "Enter in the second Number -> ";
    cin >> secondNo;

    cout<< "which operation would you like to choose?";


    // result of the operation when addition is chosen
    resultAdd = firstNo + secondNo;
    cout<< "the sum of the numbers are ->" <<resultAdd << "/n";
    cin >> resultAdd;



    // result of the operation when subtraction is chosen
    resultSub = firstNo - secondNo;
    cout<< "the difference of these numbers are ->" <<resultSub<< "/n";




    // result of the operation when multiplication is chosen
    resultMult = firstNo * secondNo;
    cout<< "the product of these numbers are ->" <<resultMult<< "/n";



    // result of the operation when division is chosen
    resultDivi = firstNo/secondNo;
    cout<< "the quotient of these two numbers are ->/n"<<resultMult<< "/n";


    if(secondNo = 0)// restriction put upon operation so that denominator cannot be 0

    cout<< "the quotient of these two numbers are undefined ";




    cout<<"/n";

    return 0;

    }
    btw i am using windows visual studio c++
    can you try compiling it and see if the same thing happens on your computer?
     
  6. Sep 6, 2010 #5

    Mark44

    Staff: Mentor

    Re: first time programming ever and my first project has 1-21 errors please help in c

    You prompt the user the operation to perform here, but the program doesn't actually allow the user to enter an operation.

    You need to input the operation, and then have logic in your program to perform that operation. One way to do this would be to use an if ... else ... control struction.
    The line above makes no sense. resultAdd is computed, not entered as input. I said that before.
    You're displaying the wrong variable above. You stored the division result in resultDivi, but are displaying resultMult. Also, you will have problems (as noted below) if secondNo is zero.
    It's too late. If secondNo is zero, your program has already attempted to divide by zero, and will throw an exception even before it gets to the line above.
    I don't need to. For simple code such as this, I can tell what will happen just be looking at it.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: First time programming ever and my first project has 1-21 errors please help in c++
Loading...