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

Click For Summary

Discussion Overview

The discussion revolves around a user's experience with programming in C++, specifically focusing on debugging a simple calculator program that performs basic arithmetic operations. Participants explore issues related to syntax errors, logical errors, and user input handling in the context of a homework assignment.

Discussion Character

  • Homework-related
  • Technical explanation
  • Exploratory

Main Points Raised

  • The user reports multiple syntax errors in their C++ code, including issues with variable declarations and input/output statements.
  • Some participants highlight the need for semicolons at the end of variable declarations and correct the use of the assignment operator versus the equality operator.
  • There is a suggestion to avoid using `cin` for outputting calculated results, as the program should display results rather than ask for user input for them.
  • One participant provides a revised version of the code, indicating that it runs but does not behave as intended, particularly in how it handles user input for operations.
  • Participants discuss the logic for handling division by zero and the need for proper conditional checks.

Areas of Agreement / Disagreement

Participants generally agree on the identification of syntax errors and the need for corrections, but there is no consensus on the final implementation of the program or its intended functionality.

Contextual Notes

Limitations include unresolved logical errors in the program, particularly regarding user input handling and the implementation of operations based on user choice. The discussion does not resolve how to properly structure the program for user interaction.

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
count<< "Enter in the first Number -> /n";
cin >> "firstNo";

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

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

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

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

// result of the operation when division is chosen
resultDivi == firstNo/seconNo;
count<< "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;
count<< "the quotient of these two numbers are undefined :(";


count<<"/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;

count<< "these numbers add up to ->";
count<< 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
count<< "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
count<< "Enter in the second Number -> ";
cin >> "secondNo";

count<< "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:
count<< "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;
count<< "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;
count<< "the product of these numbers are ->/n";
cin >> "resultMult";

// result of the operation when division is chosen
resultDivi == firstNo/seconNo;
count<< "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:
count<< "the quotient of these two numbers are undefined :(";


count<<"/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;

count<< "these numbers add up to ->";
count<< 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
count<< "Enter in the first Number ->";
cin >> firstNo;

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

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


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

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


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

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

count<<"/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
count<< "Enter in the first Number ->";
cin >> firstNo;

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

count<< "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;
count<< "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;
count<< "the difference of these numbers are ->" <<resultSub<< "/n";




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



// result of the operation when division is chosen
resultDivi = firstNo/secondNo;
count<< "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:
count<< "the quotient of these two numbers are undefined ";




count<<"/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 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 24 ·
Replies
24
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 5 ·
Replies
5
Views
2K
Replies
5
Views
2K