Multiplying two integers if initial input is an odd integer in C++ - 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

Related

c++ task won't execute after while loop

C++ newbie here. I'm not sure how to describe this but the task outside of the while-loop won't execute immediately. I need to enter the input value again to get it done.
Here is my code:
#include <iostream>
using namespace std;
int main()
{
int fourDigitInt, firstDigit, secondDigit, thirdDigit, fourthDigit, i = 0;
cout << "Enter a 4-digit integer : ";
cin >> fourDigitInt;
while (fourDigitInt > 9999 || !(cin >> fourDigitInt))
{
i++;
if (i >= 3)
{
cout << "You do not seem to understand the program instruction.\nPlease try again later." << endl;
return -1;
}
else
{
cout << "Error: Please make sure you are entering a 4-digit integer" << endl;
cin.clear();
cin.ignore(4, '\n');
cout << "Enter a 4-digit integer : ";
}
}
firstDigit = fourDigitInt / 1000 % 10;
secondDigit = fourDigitInt / 100 % 10;
thirdDigit = fourDigitInt / 10 % 10;
fourthDigit = fourDigitInt / 1 % 10;
cout << "1st digit : " << firstDigit << endl;
cout << "2nd digit : " << secondDigit << endl;
cout << "3rd digit : " << thirdDigit << endl;
cout << "4th digit : " << fourthDigit << endl;
}
Here are some problems I encountered:
1)If I enter a string first, it doesn't have any problem.
2)But if I enter any number less than 9999, it won't execute the calculation unless I
enter it again.
3)If I enter a 5-digit number the endl won't work. It will just display Enter a 4-digit integer : Error: Please make sure you are entering a 4-digit integer which suppose to be a different line.
Where exactly did I do wrong? Thank you in advance.
The main issue here is in the while loop condition. It should just check for the value of the fourDigitInt variable as that is what is important.
Looking closer, you will also be able to notice the fact that the if case inside of the loop would check for the second iteration instead of the third. I fixed that as well by moving the i++ inside of the else block.
while (fourDigitInt > 9999 || fourDigitInt < 1000) {
if (i >= 3) {
cout << "You do not seem to understand the program (ironic coming for OC) instruction.\nPlease try again later." << endl;
return -1;
}
else {
i++;
cout << "Error: Please make sure you are entering a 4-digit integer" << endl;
cout << "Enter a 4-digit integer : ";
cin >> fourDigitInt;
}
}
Any other issues with your code that may occur are not related to this question.

simple while loop calculator

Hello im stuck at SUBTRACTION AND DIVITION AND I CANT FIGURE OUT WHAT CODE to use because when I try to subtract 10 i inputed it then it will loop since the while condition is not meet which it needs to be negative to terminate the loop and i inputed 2 for the second number then loop again then i putted -number which lead to terminate loop and subtract all the number but the result is -12 its always wrong in every number cant figure out why Please help
Also with divition, only my addition is working havent started the divition cuz i cant figure out how
#include <iostream>
using namespace std;
int amt2, total;
double subNumbers();
double amt=1;
double number=0;
int main() {
int chc=0;
int amt = 0;
int amt2 = 1;
cout << "Welcome again User!\n";
cout << "______________________________________________________________\n" << endl;
cout << "Mathematical Operations(Improved):\n\n";
cout << "\t[1]-Addition" << endl;
cout << "\t[2]-Subtraction" << endl;
cout << "\t[3]-Multiplication" << endl;
cout << "\t[4]-Division\n" << endl;
cout << "______________________________________________________________\n" << endl;
cout << "Type the number corresponding to your chosen operation: ";
cin >> chc;
```
switch (chc) {
case 1:
```
system ("cls");
cout << "\n\n\tOperation chosen: Addition";
cout << "\n______________________________________________________________" << endl;
cout << "\n\nInput positive numbers to use the operation and input a negative number to end the operation.\n\n";
cout << "Enter your number: ";`
cin >> number;
while (number >= 0) {
// add all positive numbers
amt += number;
// take input again if the number is positive
cout << "Enter another number: ";
cin >> number;
}
// display the sum
cout << "\nThe sum of all the numbers is: " << amt << endl;
break;
```
case 2:
system ("cls");
cout << "\n\n\tOperation chosen: Subtraction";
cout << "\n______________________________________________________________" << endl;
cout << "\n\nInput positive numbers to use the operation and input a negative number to end the operation.\n\n";
do{
cout << "Enter your number: ";
cin >> number;
amt=number-number ;
}while (number >= 0);// subtract all positive numbers
// display the difference
cout << "\nThe difference of all the numbers is: "<<amt;
return 0;
}}
```
enter code here
You are subtracting number from number:
amt = number - number; // Which is always 0
So that's why amt == 0 always.
So just change your loop to this:
while (true) {
cout << "Enter your number: ";
cin >> number;
if (number < 0) break;
if (amt == 0) amt = number;
else if (number >= 0) amt -= number;
}
What this does is that if amt == 0, then set amt to number. I have done this because as the default value of amt is 0 (due to int amt = 0;), when amt == 0, then we can assume that the user has entered the first number, and thus we can set amt to number. And then we can use -= operator, which basically means:
amt = amt - number;
But before all this, using if (number < 0) break; we can check if the user has entered a negative number, and if the user has entered a negative number, then the break keyword will break out of the while loop.

How do I fix this calculator error in 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.

Validation While Loop Only Repeating Every Other Time

I'm creating a program that is an arithmetic quiz. The program displays a menu and presents a quiz depending on the option that the user chooses (1 for addition, 2 for subtraction, etc.).
My while loops that validate if the user answered a problem correctly or incorrectly seems to be repeating every other time as opposed to every time. This ends up affecting the correct/incorrect counters for how many questions a user answers correctly/incorrectly, and I haven't been able to figure out why this is happening - what do I need to fix?
I am also having issues where the loop doesn't end even if the user enters -1 to stop.
#include <cstdlib>
#include <ctime>
using namespace std;
// function prototypes
void showMenu();
int add(int, int);
// main function
int main()
{
int choice = 0, answer = 0; // user input
int num1, num2; // numbers for problem
int correct_sum; // correct answer
int correct = 0, incorrect = 0; // counters
do
{
// display program purpose to user
cout << "********* Welcome to the Arithmetic Quiz *******\n";
cout << '\n';
// call showMenu function
showMenu();
cin >> choice; // user inputs menu choice
cout << '\n';
// switch statement for menu choice
switch (choice)
{
// addition
case 1:
do {
// randomize numbers
srand(time(NULL)); // random seed
num1 = rand() % 9 + 1; // random num1
num2 = rand() % 9 + 1; // random num2
// display problem to user
cout << "How much is " << num1 << " plus " << num2 << "?\n";
cout << "Enter your answer (-1 to return to the menu)\n";
cin >> answer;
cout << '\n';
// correct sum formula & call addition() function
correct_sum = add(num1, num2);
// while user's answer is WRONG and not -1
while (answer != correct_sum && answer != -1)
{
// display incorrect message
cout << "No. Please try again.\n";
incorrect++; // increment incorrect counter
// ask user for input again
cout << "Enter your answer (-1 to return to the menu)\n";
cin >> answer;
cout << '\n';
}
// while user's answer is RIGHT
while (answer == correct_sum)
{
// display correct message
cout << "Very Good!\n";
correct++; // increment correct counter
// randomize numbers again for different problem
srand(time(NULL)); // random seed
num1 = rand() % 9 + 1; // random num1
num2 = rand() % 9 + 1; // random num2
// show more problems to user until -1 is entered
cout << "How much is " << num1 << " plus " << num2 << "?\n";
cout << "Enter your answer (-1 to return to the menu)\n";
cin >> answer;
cout << '\n';
}
} while (answer != -1);
if (answer == -1)
{
// display addition results when answer == STOP
cout << "********* RESULTS *******\n";
cout << '\n';
cout << "Addition problems solved: " << (correct + incorrect) << '\n';
cout << "Number answered correctly: " << correct << '\n';
cout << "Number answered incorrectly: " << incorrect << '\n';
}
}
// --------------------------------------------------------------------------------------
// showMenu function
void showMenu()
{
// display menu options
cout << "MENU:\n";
cout << '\n';
cout << "1. Enter 1 for Addition\n";
cout << "2. Enter 2 for Subtraction\n";
cout << "3. Enter 3 for Multiplication\n";
cout << "4. Enter 4 for Division\n";
cout << "5. Enter 5 for Modulus\n";
cout << "6. Enter 6 to Exit\n";
cout << '\n';
}
// --------------------------------------------------------------------------------------
// add function
int add(int num1, int num2)
{
return (num1 + num2);
}
You don't have yo use while loop inside do..while loop, if condition will work there, Try below code i have commented some part of code.
#include<iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
// function prototypes
void showMenu();
int add(int, int);
// showMenu function
void showMenu()
{
// display menu options
cout << "MENU:\n";
cout << '\n';
cout << "1. Enter 1 for Addition\n";
cout << "2. Enter 2 for Subtraction\n";
cout << "3. Enter 3 for Multiplication\n";
cout << "4. Enter 4 for Division\n";
cout << "5. Enter 5 for Modulus\n";
cout << "6. Enter 6 to Exit\n";
cout << '\n';
}
// --------------------------------------------------------------------------------------
// add function
int add(int num1, int num2)
{
return (num1 + num2);
}
// main function
int main()
{
int choice = 0, answer = 0; // user input
int num1, num2; // numbers for problem
int correct_sum; // correct answer
int correct = 0, incorrect = 0; // counters
do
{
// display program purpose to user
cout << "********* Welcome to the Arithmetic Quiz *******\n";
cout << '\n';
// call showMenu function
showMenu();
cin >> choice; // user inputs menu choice
cout << '\n';
// switch statement for menu choice
switch (choice)
{
// addition
case 1:
do {
// randomize numbers
// srand(time(NULL)); // random seed
num1 = rand() % 9 + 1; // random num1
num2 = rand() % 9 + 1; // random num2
// display problem to user
cout << "How much is " << num1 << " plus " << num2 << "?\n";
cout << "Enter your answer (-1 to return to the menu)\n";
cin >> answer;
cout << '\n';
// correct sum formula & call addition() function
correct_sum = add(num1, num2);
// while user's answer is WRONG and not -1
while(answer != correct_sum && answer != -1)
{
// display incorrect message
cout << "No. Please try again.\n";
incorrect++; // increment incorrect counter
// ask user for input again
cout << "Enter your answer (-1 to return to the menu)\n";
cin >> answer;
correct_sum = add(num1, num2);
cout << '\n';
}
// while user's answer is RIGHT
if (answer == correct_sum)
{
// display correct message
cout << "Very Good!\n";
correct++; // increment correct counter
// randomize numbers again for different problem
// srand(time(NULL)); // random seed
num1 = rand() % 9 + 1; // random num1
num2 = rand() % 9 + 1; // random num2
// show more problems to user until -1 is entered
//cout << "How much is " << num1 << " plus " << num2 << "?\n";
//cout << "Enter your answer (-1 to return to the menu)\n";
//cin >> answer;
cout << '\n';
}
} while (answer != -1);
if (answer == -1)
{
// display addition results when answer == STOP
cout << "********* RESULTS *******\n";
cout << '\n';
cout << "Addition problems solved: " << (correct + incorrect) << '\n';
cout << "Number answered correctly: " << correct << '\n';
cout << "Number answered incorrectly: " << incorrect << '\n';
}
}
}while (answer != -1);
}
// --------------------------------------------------------------------------------------
Output of above code now if answer is not correct
********* Welcome to the Arithmetic Quiz *******
MENU:
1. Enter 1 for Addition
2. Enter 2 for Subtraction
3. Enter 3 for Multiplication
4. Enter 4 for Division
5. Enter 5 for Modulus
6. Enter 6 to Exit
1
How much is 6 plus 9?
Enter your answer (-1 to return to the menu)
10
No. Please try again.
Enter your answer (-1 to return to the menu)
10
No. Please try again.
Enter your answer (-1 to return to the menu)
10
No. Please try again.
Enter your answer (-1 to return to the menu)
10
No. Please try again.
Enter your answer (-1 to return to the menu)
10
No. Please try again.
Enter your answer (-1 to return to the menu)
10
No. Please try again.
Enter your answer (-1 to return to the menu)
10
I hope you are expecting this.

Simple calculator issues (Not outputting answers)

just started reading a C++ book and one of the practice problems was to write a small calculator that takes as input one of the four arithmetic operations, the two arguments to those operations, and then prints out the results.
Sadly, the program works up until the user inputs the arithmetic option.
So if I chose to do multiplication, id write "Multiplication" and it was just stay there and not do anything after.
Image of the problem im having
#include <iostream>
#include <string>
using namespace std;
int main(){
// Simple calculator program
// Declaring three variables
float numberOne;
float numberTwo;
string operationOption;
// Asking the user which two numbers he/she will use
cout << "Enter the first number you would like to apply a arithmetic operation to: ";
cin >> numberOne;
cin.ignore();
cout << "Now enter the second number: ";
cin >> numberTwo;
cin.ignore();
// Using cin to input users selection
cout << "Enter the operation you want to perform." << endl;
cout << "The options you have are: " << endl;
cout << "Multiplication, Subraction, Division and Addition: " << endl;
cin >> operationOption;
cin.ignore();
cin.get();
// Where it all happens
if ( operationOption == "Multiplication" ) {
cout << "The first number multiplied by the second number is: " << numberOne * numberTwo << endl;
} else if ( operationOption == "Division" ) {
cout << "The first number divided by the second number is: " << numberOne / numberTwo << endl;
} else if ( operationOption == "Subtraction" ) {
cout << "The first number subtracted by the second number is: " << numberOne - numberTwo << endl;
} else if ( operationOption == "Addition ") {
cout << "The first number added to the second number is: " << numberOne + numberTwo << endl;
} else {
cout << "You entered an invalid option.";
};
}
Remove line :
cin.get();
will solve your problem