Variable Holding data in a while statement - c++

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?

Related

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.

How to check whether the program goes over the data type storage?

I need to find out how to control the flow of the program so that the program does not crash when the user inserts huge numbers. The goal of the program is to calculate the Fibonacci series, by the user's input.
#include <iostream>
using namespace std;
int main() {
int n;
cout << " Please enter number of fibonacci number series you wanna see ";
cin >> n;
int num1=0;
int num2=1;
cout << num1 << " " << num2 << " ";
if(num1+num2<=32756)
for (int i=0; i<=n-1; ++i)
{
int num2Temp=num2;
cout << num1+num2 << " ";
num2=num1+num2;
num1=num2Temp;
}
else
cout << " TOO BIG " << endl;
return 0;
}
I do calculate the Fibonacci series, however, it does not follow the if statement and stops the program as it exceeds the 32756. I understand why it does it as the num1+num2 is not updated, so that program understand it 0+1, no the update versions for each loop. But I do not have a specific idea to fix the program.
You only test the sum once, before your loop starts.
You probably want to test inside the loop, and exit it if the sum exceeds 32756:
for (int i = 0; i < n; i++)
{
if (num1 + num2 <= 32756)
{
int num2Temp = num2;
num2 += num1;
num1 = num2Temp;
cout << num2 << " ";
}
else
{
cout << " TOO BIG " << endl;
break;
}
}

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

For loop not being executed second time around in a while loop

I'm just a beginner in C++. I'm writing small and simple program that prints a series of integers between two user-specified integers.
At the end, the program will re-run the while loop if the user returns 1, but when that happens the program will not print the series of numbers again (the for loop doesn't work).
Here's the source code:
int main(void)
{
int num1, num2;
int doContinue = 1;
while (doContinue == 1)
{
cout << "Please enter two integers, the first being the smallest: ";
do { //does everything in curly braces while the user inputs the numbers wrong...
cin >> num1 >> num2;
if (num1 > num2)
{
cout << "Your first number was bigger than the second.\nTry again!: ";
}
} while (num1 > num2);//... but once it's not wrong, break out of this do loop
//at this point the input has been checked, so we can proceed to print the series
for(int num1; num1 <= num2; num1++)
{
cout << num1 << " \n";
}
cout << "Would you like to compute another series of integers? 1=yes, anything else=no: ";
cin >> doContinue;
}
return 0;
}
Your code exhibits undefined behavior.
for(int num1; num1 <= num2; num1++)
{
cout << num1 << " \n";
}
creates a new integer called num1, unrelated to the num1 outside of the for loop. You do not initialize the value of num1, but proceed to make comparisons against it.
Remove int num1 (that is, something like for(; num1 <= num2; num1++)) in your for loop and try again.
Try
for(int i = num1; i<= num2; i++)
instead of
for(int num1; num1 <= num2; num1++)
change this part of code
for(int num1; num1 <= num2; num1++)
{
cout << num1 << " \n";
}
to
for( ; num1 <= num2; num1++)
{
cout << num1 << " \n";
}