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

AI Thread Summary
The discussion revolves around a user's first programming project in C++, which has multiple syntax and logical errors. Key issues include improper variable declarations, incorrect use of assignment versus comparison operators, and the misuse of input/output functions. The user is prompted to choose an operation but does not implement logic to handle user input for operations, leading to confusion in the program's flow. Additionally, there are errors related to division by zero that need to be addressed before executing the division operation. Overall, the user is encouraged to revise their code to correct these errors and improve functionality.
ihatecats2014
Messages
30
Reaction score
0

Homework Statement


#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 a lot 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:
Physics news on Phys.org


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:


ihatecats2014 said:

Homework Statement





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

ihatecats2014 said:
// asks the user to choose the first number
cout<< "Enter in the first Number -> /n";
cin >> "firstNo";
You need a variable, not a string literal. Presumably this is why you declared firstNo. Similar comment on the next two groups of statements.
ihatecats2014 said:
// 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;
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.
ihatecats2014 said:
cout<< "the sum of the numbers are ->/n";
cin >> "resultAdd";
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.
ihatecats2014 said:
// result of the operation when subtraction is chosen
resultSub = firstNo - secondNo;
cout<< "the difference of these numbers are ->/n";
cin >> "resultSub";
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.
ihatecats2014 said:
// 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;
What do you think this is doing?
ihatecats2014 said:
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 a lot 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.
 


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?
 


ihatecats2014 said:
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?";
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.
ihatecats2014 said:
// result of the operation when addition is chosen
resultAdd = firstNo + secondNo;
cout<< "the sum of the numbers are ->" <<resultAdd << "/n";
cin >> resultAdd;
The line above makes no sense. resultAdd is computed, not entered as input. I said that before.
ihatecats2014 said:
// 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";
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.
ihatecats2014 said:
if(secondNo = 0)// restriction put upon operation so that denominator cannot be 0
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.
ihatecats2014 said:
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?

I don't need to. For simple code such as this, I can tell what will happen just be looking at it.
 

Similar threads

Replies
2
Views
2K
Replies
6
Views
3K
Replies
3
Views
1K
Replies
9
Views
2K
Replies
14
Views
4K
Back
Top