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

In summary, the program has errors in three places: declaring secondNo, resultAdd, and resultSub; asking for the sum of the numbers as input; and displaying the calculated result.
  • #1
ihatecats2014
30
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
  • #2


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:
  • #3


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.
 
  • #4


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?
 
  • #5


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.
 

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

1. What are the most common errors in C++ programming for beginners?

The most common errors for beginners in C++ programming include syntax errors, such as missing semicolons, incorrect capitalization, and incorrect use of parentheses or brackets. Other common errors include logic errors, which occur when the code does not produce the expected output, and runtime errors, which occur during the execution of the program.

2. How can I fix errors in my C++ code?

To fix errors in your C++ code, you should carefully review the error messages and check for any syntax or logic errors. You can also use debugging tools to step through your code and identify where the errors are occurring. Additionally, asking for help from more experienced programmers or referring to online resources can also assist in resolving errors.

3. Why am I getting so many errors in my first C++ project?

It is common for beginners to encounter multiple errors in their first C++ project because programming requires attention to detail and a strong understanding of syntax and logic. It is important to carefully review each error message and make corrections as needed to improve your code.

4. Are there any tips for avoiding errors in C++ programming?

Some tips for avoiding errors in C++ programming include regularly testing your code as you write it, commenting your code for better organization and understanding, and using debugging tools to identify and fix errors. It is also helpful to practice and familiarize yourself with common syntax and programming concepts.

5. How can I become a better C++ programmer and reduce errors in my projects?

To become a better C++ programmer and reduce errors in your projects, it is important to continuously practice and learn new skills. You can also benefit from working with more experienced programmers and seeking feedback on your code. Additionally, regularly reviewing and refactoring your code can help improve its efficiency and reduce the likelihood of errors.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
14
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
19
Views
2K
  • Programming and Computer Science
2
Replies
39
Views
3K
Back
Top