How do I fix this calculator error in c++? - c++

I'm new to programming and I came across this problem im not sure why the output is negative can someone explain?
Edit: Thanks for the help!
#include <iostream>
using namespace std;
int main()
{
cout << "Enter your first number " << endl;
int num1;
cin >> num1;
cout << "Enter your second number" << endl;
int num2;
cin >> num2;
cout << "Would you like to add a third number, if your answer is yes enter yes, if your
answer is no enter no" << endl;
int yes;
cin >> yes;
yes = 1;
int sum;
if (yes == 1) {
cout << "Enter another number" << endl;
int num3;
cin >> num3;
sum = num1 + num2 + num3;
cout << "Sum = " << sum << endl;
} return 0;
output: Enter another number
Sum =-858993440

Your issue is coming from this check
if (yes == 1) {
What you want to be checking for is a std::string instead. Since the inputted type is a string you need to take it as a string instead of a integer.
Something like this should work for you:
int main() {
int num1 = 0, num2 = 0, sum = 0;
std::string yes;
std::cout << "Enter your first number " << std::endl;
std::cin >> num1;
std::cout << "Enter your second number" << std::endl;
std::cin >> num2;
std::cout << "Would you like to add a third number, if your answer is yes enter yes, if you answer is no enter no"
<< std::endl;
std::cin >> yes;
if (yes == "yes") {
std::cout << "Enter another number" << std::endl;
int num3 = 0;
std::cin >> num3;
sum += num3;
}
sum += num1 + num2;
std::cout << "Sum = " << sum << std::endl;
return 0;
}

From your program, I am guessing that you want to add multiple numbers together. The problem is, you are expecting a string and scanning an int! That is what I would term undefined behaviour. Not to mention if you are going to assign it a value explicitly just after scanning it, you probably do not even want to scan it.
This is how you expect a string and scan one:
std::string choice;
do {
//whatever the hell you wanna do!
std::cin >> choice;
} while (choice == "yes");

You input a data onto the variable yes in the wrong datatype.
So I think the standard I/O thread has crashed and cause you can not give a value to the variable num3. And the num3 is not initialized. The memory maybe still has a uncertain value that makes this result.

Related

error: no match for 'operator-' When trying to subtract strings

I am trying to subtract these two numbers, and I keep getting this error.
code:
#include <iostream>
using namespace std;
void Cal()
{
string Money = "back";
string TotPay = "back";
string Assets = "back";
cout << "At anytime, you can type the word 'back' to go back a step." << endl;
do{
cout << "Enter your total amount of money: ";
cin >> Money;
}while(Money == "back");
do{
cout << "\nEnter the total of your payments: ";
cin >> TotPay;
}while(TotPay == "back");
do{
cout << "\nEnter the total value of your assets: ";
cin >> Assets;
}while(Assets == "back");
cout << "Your net worth is " << (Money + Assets) - TotPay << "!";
}
int main()
{
string name;
string yn;
cout << "Enter your name please: ";
cin >> name;
cout << "\nHello " << name << ", today we are going to calculate your net worth.\n";
do{
Cal();
cout << "Would you like to calculate again? (yes/no)\n";
cin >> yn;
}while(yn == "yes");
cout << "See ya!";
return 0;
}
The error is on the line of code that says
cout << "Your net worth is " << (Money + Assets) - TotPay << "!";
The program will not ever compile with the subtraction sign (-) there, and if I change it to a addition sign (+) then it will compile, but the numbers will not properly add, and the end number just ends up being a place holder number for the variable (for example, 2555354 or something like that).
Am I missing something here?
Any help would be appreciated.
Although others have pointed out rightly..
To get this code working, the string values need to be treated as an integer:
cout << "Your net worth is " << (stoi(Money) + stoi(Assets)) - stoi(TotPay) << "!";
You are trying to add and subtract strings, not numbers.
While addition works (and results in string concatenation), subtracting strhings doesn't make sense. Use numbers:
int Money = 0;
int TotPay = 0;
int Assets = 0;
Depending on your applications need, you might want to use a different numeric type, e.g. double.

My program is not asking for input

I am writing a program and it is a long program and I have just started. I just tested it for running, Please tell me why it is not having user input:
#include <iostream>
using namespace std;
struct courses{
int CLO1, CLO2, CLO3, CLO4, CLO5;
int tcounter;
};
int main(){
cin.clear();
courses C1;
C1.CLO1;
C1.CLO2;
C1.CLO3;
int counter = 0;
char name;
cout << "Enter your Name: ";
cin >> name;
cout << "For C1, we have three CLOs that CLO1,CLO2 and CLO3. CLO1 and CLO3 are linked to PLO1 and CLO2 is linked to PLO2 " << endl;
cout << "Enter your marks in CLO1 for C1:(Out of 10) ";
cin >> C1.CLO1;
if (C1.CLO1 >= 5){
counter++;
}
cout << "Enter your marks in CLO2 for C1:(Out of 10) ";
cin >> C1.CLO2;
if (C1.CLO2 >= 5){
counter++;
}
cout << "Enter your marks in CLO3 for C1:(Out of 10) ";
cin >> C1.CLO3;
if (C1.CLO3 >= 5){
counter++;
}
cout << counter;
return 0;
}
As per me the program is fine.
Three things you need to change:
variable type of name from char to string
C1.CLO1;C1.CLO2;C1.CLO3; remove these from your code it doesn't make any sense.
Print the values and check :P

How to ask for user input in nested loops?

I was given the task of displaying Fibonacci numbers, but while asking the user how many number he/she would like to compute at a given time.
There was an example in the book they told me to refer. I figured a few lines of change in the code would produce the answer to my problem, but I'm having trouble understanding where I went wrong with this code.
int main()
{
int NumsToCal = 5;
cout << "How many numbers would you like to calculate?" << endl;
cin >> NumsToCal;
cout << " This program will calculate " << NumsToCal << " Fibonacci Numbers at a time" <<endl;
int Num1 = 0, Num2 = 1;
char WantMore = '\0';
cout << Num1 << " " << Num2 << " " ;
do
{
for( int Index = 0; Index < NumsToCal; ++Index)
{
cout << Num1 + Num2 << " ";
int Num2Temp = Num2;
Num2 = Num1 + Num2;
Num1 = Num2Temp;
}
cout << "Do you want more numbers (y/n)? " << endl;
cin >> WantMore;
} while (WantMore == 'y');
cout << "Goodbye!" << endl;
return 0;
}
Xsami is absolutely right. You only need to include one more line like:
cin>>NumstoCal;
Though it won't be bad to change the way you output stuff for a bit more clarity.
Here is my code:
https://ideone.com/BXREP9
The only thing that you have to do is read NumsToCal again, and you have to do something like this after cin >> WantMore;
if ( WantMore == 'y' )
{
Num1 = 0;
Num2 = 1;
cout << "How many numbers would you like to calculate?" << endl;
cin >> NumsToCal;
cout << Num1 << " " << Num2 << " " ;
}
This is my code: http://ideone.com/a8um5Z

Multiplying two integers if initial input is an odd integer in C++

I'm working on a homework assignment for a beginning C++ class and I'm a bit lost.
Here's the assignment:
Create a c++ program which ask the user to input a number.
The output of the program should be one of the following:
You entered an EVEN number.
OR
You entered an ODD number.
if the user entered an ODD number, ask them to enter another number.
Multiply this number by the first number and output the result.
The even/odd part is pretty easy- I got that part to work. I've gotten completely lost on the second part. I'm getting so many lines of errors I can't even figure out where the beginning is. If anyone could give me a hint as to what I'm doing wrong, I'd greatly appreciate it.
#include <iostream>
using namespace std;
int main () {
int num1; // This is the original number entered by the user.
int num2; // This is the second number entered if the first number is odd.
cout << "Enter a number: "<< endl;
cin >> num1 >> endl;
if (num1 % 2 == 0) {
cout << num << " Your number is even." << endl;
} if (num1 % 2 != 0) {
cout << num1 << " Your number is odd. Please enter another number: “<< endl;
cin >> num1 >> endl;
} // end of if odd
cout << " Your two numbers multiplied equals (num1 *= num2)” << endl;
} // end of main ()
#include <iostream>
using namespace std;
int main () {
int num1; // This is the original number entered by the user.
int num2; // This is the second number entered if the first number is odd.
cout << "Enter a number: "<< endl;
cin >> num1;
if (num1 % 2 == 0) {
cout << num1 << " Your number is even." << endl;
}
else {
cout << num1 << " Your number is odd. Please enter another number: " << endl;
cin >> num2;
cout << " Your two numbers multiplied equals " << num1*num2 << endl;
} // end of if odd
return 0;
} // end of main ()
Here's fixed code. You tried to cout << num, but there's no num variable, should be num1, also it's wrong to cin >> endl.
What was unexpected, your ” at the end is not a " but something else and it produces a lot of errors.
The part for the odd values
if (num1 % 2 != 0) {
cout << num1 << " Your number is odd. Please enter another number: “<< endl;
cin >> num2 >> endl;
cout << " Your two numbers multiplied equals:" << (num1 * num2) << endl;
}
after corrections
if (num1 % 2 != 0) {
cout << num1 << " Your number is odd. Please enter another number:"<< endl;
cin >> num2;
cout << " Your two numbers multiplied equals:" << (num1 * num2) << endl;
}
do not place formulas between quotaion marks. this turns them into strings or chars that cant be executed as desire. ie cout << " Your two numbers multiplied equals (num1 *= num2)” << endl;
placing the statement cout << " Your two numbers multiplied equals (num1 *= num2)” << endl; out side the if statement causes the statement to be run even if the number was not odd. this doesnt comply with the assignment. and num2 being null still will cause an error

Variable Holding data in a while statement

I know I must be missing something, but in a while statement how does the variable hold the data, when it finishes the first pass and goes into the second pass?
{
int num1 = 0 ;
int num2 = 0;
int num3 = 0;
while (num1 < 10)
{cout << "enter your first number: ";
cin >> num1;
cout << "Enter your second number: ";
cin >> num2;
num1 = num1 + num2 ;
cout << "Number 1 is now: " << num1 <<endl;
cout << "Enter Number 3: " ;
cin >> num3;
num1 = num1 + num3;
cout << "Number 1 is now: " << num1 << endl;
num1++;
};
In this code. The Variable doesn't hold the data. I'm not sure what I'm doing wrong!
Is num1 the variable you're having trouble with? This line:
cin >> num1;
is setting num1 to the value input by the user. So the value calculated for it in the previous run through the loop is being overwritten each time by the new input.
I'm not clear exactly what you're asking, but variables will maintain their value for each iteration of a loop, as long as they're declared outside of the loop itself. For example:
int a = 0;
while(a < 10)
{
int b = 0;
cout << "a: " << a << " b: " << b << "\n";
a++;
b++;
}
In the above, the value output for b will always be 0, as it's declared inside the loop and is being reinitialized each time, whereas a will maintain its value and get incremented each iteration. If b were an object, rather than an int, its constructor and destructor would get called each iteration.
I'm not sure I understand your question. In C any data that's not overwritten is carried over into the next iteration of the loop, and imagine that C++ works much the same way.
Do you understand how when you say "num1" you're referring to the same variable each time, and that each time you change num1 you replace the previous value?