// FOR-LOOP.CPP
// ADD & TOTAL-UP NUMBERS PROGRAM:
#include <iostream>
using namespace std;
int main()
{ // start brace
int a;
int b;
int c;
for(int a=1;a<=3;a++)
{
system("cls");
cout <<"\nKey a number:" << endl;
cin >> b;
}
return 0;
} // end brace
// Can anyone please help me to solve this for loop?
// I am trying to add up numbers and total them.
Note! I can do it with a while loop, but can not get it to work with the for loop.
----------------------------------------------------------------------------
Note! This while loop work like it suppose to do.
// WHILE-TEST.CPP
// ADD @ TOTAL-UP NUMBERS PROGRAM:
#include <iostream>
using namespace std;
int main()
{ // start
int a = 0;
int b = 5;
cout << "\nKey a number: Or key any letter to end" << endl;
while (b>=4)
{
cout << "Ok>";
cin >> b;
a = b + a;
}
{
cout << "Grand total is: " << a;
}
return 0;
}
// WHILE-TEST.CPP
// ADD @ TOTAL-UP NUMBERS PROGRAM:
#include <iostream>
usingnamespace std;
int main()
{ // start
int a = 0;
int b = 5;
cout << "\nKey a number: Or key any letter to end" << endl;
while (b>=4)
{
cout << "Ok>";
cin >> b;
a = b + a;
}
{
cout << "Grand total is: " << a;
}
return 0;
}
...more or less.
Here’s an example of its output:
Key a number: Or key any letter to end
Ok>3
Grand total is: 3
I think you should decide if you want your loop to stop after a fixed number of iterations or on user input. Once your original while-loop works properly, I’m pretty sure you’ll be able to turn it into a for-loop with a minimum effort.
- - - Edit: original OP's code included inside quote tags
Enoizat,
Let me just "refactor" your source code a bit ^_^
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <iostream>
int main()
{
intconst whatIsThisValue = 4;
int grandTotal = 0;
std::cout << "\nKey a number: Or key any letter to end" << std::endl;
for(auto userInput = 1 + whatIsThisValue; whatIsThisValue < userInput;)
{
std::cout << "Enter a number>";
std::cin >> userInput;
grandTotal += userInput;
}
std::cout << "Grand total is: " << grandTotal << std::endl;
//--
return 0;
}
I didn't know what the value 4 is by the value :oP~
Samtheman> be careful. When you write "int a" in a for(..) definition, you define a local variable a. If ever you have a "global scope" level variable named "a" also, that variable will be masked by the local one --> you will not change the value of that global variable a.
For that you just have to write for(a = ...) and forget about the "int", because you already declared your "a" variable.
But as it is a bad smell to only declare a variable, take the habit from now on to ever give any variable a value whenever possible : int a = 0;
This is particularly important for pointers int* p = ...;
And when you don't know the value of the pointer int* p = nullptr; is the best choice ;o)
Enoizat,
Let me just "refactor" your source code a bit ^_^
You're right, punksheep, my post wasn't clear.
That wasn't my code, it was OP's one. I was just pointing out it didn't work properly.
Thank you for your help.