can u guys please post an example of a calculator that allows you to enter how many numbers you want to add...i just wanna see cause im learning about loops in my book now..heres what i can do so far...plus i can make a calculator
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream>
#include <cstdlib>
usingnamespace std;
int main()
{
int numberofloops;
cout << "how many loops do you want to use?" << endl;
cin >> numberofloops;
for(; numberofloops > 0 ;)
{
numberofloops = numberofloops - 1;
cout << "only " << numberofloops << " loops to go! " << endl;
}
system("pause");
return 0;
}
#include <iostream>
usingnamespace std;
int main()
{
int Total = 0;
int Amount;
int Number;
cout << "Enter in how many numbers you want to use" << endl;
cin >> Amount;
for(int i = 0; i < Amount; i++) // Keeps looping until i == Amount
{
cout << "Number " << i << ": " ; // Shows user what number they are entering
cin >> Number;
Total += Number; // Adds number to total
}
cout << "The total is " << Total << endl; // Prints out total value when loop has finished
system("PAUSE");
return 0;
}
When using a for loop you usually set a beginning value (in this case int i = 0) then compare it to another value (i < Amount), then increment or decrement the initial value if the middle condition isn't met.
Another way you could do it is just keep adding numbers until the user entered 0 (or some other number) using a do-while loop.
#include <iostream>
usingnamespace std;
int main()
{
int Total = 0;
int i = 0;
int Number;
cout << "Enter in 0 to finish" << endl;
do
{
cout << "Number " << i << ": " ;
cin >> Number;
i++;
Total += Number;
}
while(Number != 0);
cout << "The total is " << Total << endl;
system("PAUSE");
return 0;
}
How bout instead of using a loop that determines how many inputs, just use a couple cleverly used variables that hold current number, a placeholder, and a current answer variable. Use a case statement for the operator selection. And you can loop through this until the user decides to quit :) Hope this helps. I'm an my iPhone so I can't really post an example
ok and can you please explain to me why this doesnt work?? -,* and / dont work...only + works....i switched Total = Number + Total to Total = Number * Total
#include <iostream>
#include <cstdlib>
usingnamespace std;
int main()
{
int Total = 0;
int Amount;
int Number;
int whatoperation;
cout << "Here are your choices(enter the number before the operation)" << endl << endl;
cout << "1.Addition" << endl;
cout << "2.Subtraction" << endl;
cout << "3.Multiplication" << endl;
cout << "4.Division" << endl;
cin >> whatoperation;
switch(whatoperation){
case 1:
cout << "Enter in how many numbers you want to use" << endl;
cin >> Amount;
for(int i = 0; i < Amount; i++) // Keeps looping until i == Amount
{
cout << "Number " << i << ": " ; // Shows user what number they are entering
cin >> Number;
Total = Number + Total; // Adds number to total
}
cout << "The total is " << Total << endl; // Prints out total value when loop has finished
break;
case 2:
cout << "Enter in how many numbers you want to use" << endl;
cin >> Amount;
for(int i = 0; i < Amount; i++) // Keeps looping until i == Amount
{
cout << "Number " << i << ": " ; // Shows user what number they are entering
cin >> Number;
Total = Number * Total; // Adds number to total
}
cout << "The total is " << Total << endl; // Prints out total value when loop has finished
break;
case 3:
cout << "Enter in how many numbers you want to use" << endl;
cin >> Amount;
for(int i = 0; i < Amount; i++) // Keeps looping until i == Amount
{
cout << "Number " << i << ": " ; // Shows user what number they are entering
cin >> Number;
Total = Number / Total; // Adds number to total
}
cout << "The total is " << Total << endl; // Prints out total value when loop has finished
break;
case 4:
cout << "Enter in how many numbers you want to use" << endl;
cin >> Amount;
for(int i = 0; i < Amount; i++) // Keeps looping until i == Amount
{
cout << "Number " << i << ": " ; // Shows user what number they are entering
cin >> Number;
Total = Number + Total; // Adds number to total
}
cout << "The total is " << Total << endl; // Prints out total value when loop has finished
break;
}
system("pause");
return 0;
}
#include <iostream>
#include <cstdlib>
usingnamespace std;
int main()
{
int Total = 0;
int Amount;
int Number;
int whatoperation;
cout << "Here are your choices(enter the number before the operation)" << endl << endl;
cout << "1.Addition" << endl;
cout << "2.Subtraction" << endl;
cout << "3.Multiplication" << endl;
cout << "4.Division" << endl;
cin >> whatoperation;
switch(whatoperation){
case 1:
cout << "Enter in how many numbers you want to use" << endl;
cin >> Amount;
for(int i = 0; i < Amount; i++) // Keeps looping until i == Amount
{
cout << "Number " << i << ": " ; // Shows user what number they are entering
cin >> Number;
Total = Number + Total; // Adds number to total
}
cout << "The total is " << Total << endl; // Prints out total value when loop has finished
break;
case 2:
cout << "Enter in how many numbers you want to use" << endl;
cin >> Amount;
for(int i = 0; i < Amount; i++) // Keeps looping until i == Amount
{
cout << "Number " << i << ": " ; // Shows user what number they are entering
cin >> Number;
Total = Number - Total; // Adds number to total
}
cout << "The total is " << Total << endl; // Prints out total value when loop has finished
break;
case 3:
cout << "Enter in how many numbers you want to use" << endl;
cin >> Amount;
for(int i = 0; i < Amount; i++) // Keeps looping until i == Amount
{
cout << "Number " << i << ": " ; // Shows user what number they are entering
cin >> Number;
Total = Number * Total; // Adds number to total
}
cout << "The total is " << Total << endl; // Prints out total value when loop has finished
break;
case 4:
cout << "Enter in how many numbers you want to use" << endl;
cin >> Amount;
for(int i = 0; i < Amount; i++) // Keeps looping until i == Amount
{
cout << "Number " << i << ": " ; // Shows user what number they are entering
cin >> Number;
Total = Number / Total; // Adds number to total
}
cout << "The total is " << Total << endl; // Prints out total value when loop has finished
break;
}
system("pause");
return 0;
}
Your subtraction case isn't working correctly because you are subtracting the total from the number each time, when it is meant to be the other way around. At line 46 change it to Total = Total - Number;
(You should also fix this for the other cases)
When looking at the multiplication and division cases for a while you should be able to see why they aren't working correctly. The total is starting at 0 and then is multiplied by whatever number the user enters, which is always going to result in an answer of 0. There are probably better ways to solve this (involving arrays or vectors) but just to stay with simple loops for now. Change your multiplication case to
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
for(int i = 0; i < Amount; i++) // Keeps looping until i == Amount
{
cout << "Number " << i << ": " ;
cin >> Number;
if(i == 0)
{
Total = Total + Number;
}
else
{
Total = Total * Number;
}
}
This makes it so the first number the user enters in the loop is simply added to the total, then everything after that is multiplied together (solves the 0 total problem). Pretty much the same thing should be done for division. The final thing you will need to do is change your Total variable to a float so the division will work correctly (it will only print out whole numbers with int).
Hopefully all of that makes sense after you try it for a while.
#include <iostream>
#include <cstdlib>
usingnamespace std;
int main()
{
int Total = 0;
int Amount;
int Number;
int whatoperation;
int restart;
loop:
system("CLS");
cout << "Here are your choices(enter the number before the operation)" << endl << endl;
cout << "1.Addition" << endl;
cout << "2.Subtraction" << endl;
cout << "3.Multiplication" << endl;
cout << "4.Division" << endl;
cin >> whatoperation;
switch(whatoperation){
case 1:
cout << "Enter in how many numbers you want to use" << endl;
cin >> Amount;
for(int i = 0; i < Amount; i++) // Keeps looping until i == Amount
{
cout << "Number " << i << ": " ; // Shows user what number they are entering
cin >> Number;
Total = Number + Total; // Adds number to total
}
cout << "The total is " << Total << endl; // Prints out total value when loop has finished
break;
case 2:
cout << "Enter in how many numbers you want to use" << endl;
cin >> Amount;
for(int i = 0; i < Amount; i++) // Keeps looping until i == Amount
{
cout << "Number " << i << ": " ;
cin >> Number;
if(i == 0)
{
Total = Total + Number;
}
else
{
Total = Total - Number;
}
}
cout << "The total is " << Total << endl; // Prints out total value when loop has finished
break;
case 3:
cout << "Enter in how many numbers you want to use" << endl;
cin >> Amount;
for(int i = 0; i < Amount; i++) // Keeps looping until i == Amount
{
cout << "Number " << i << ": " ;
cin >> Number;
if(i == 0)
{
Total = Total + Number;
}
else
{
Total = Total * Number;
}
}
cout << "The total is " << Total << endl; // Prints out total value when loop has finished
break;
case 4:
cout << "Enter in how many numbers you want to use" << endl;
cin >> Amount;
for(int i = 0; i < Amount; i++) // Keeps looping until i == Amount
{
cout << "Number " << i << ": " ;
cin >> Number;
if(i == 0)
{
Total = Total + Number;
}
else
{
Total = Total / Number;
}
cout << "The total is " << Total << endl; // Prints out total value when loop has finished
}
break;
}
cout << "enter 1 to restart or 2 to quit: ";
cin >> restart;
if(restart == 1)
goto loop;
system("pause");
return 0;
}
#include <iostream>
#include <cstdlib>
usingnamespace std;
int main()
{
int Total = 0;
int Amount;
int Number;
int whatoperation;
int answertoaverage;
int restart;
loop:
system("CLS");
cout << "Here are your choices(enter the number before the operation)" << endl << endl;
cout << "1.Addition" << endl;
cout << "2.Subtraction" << endl;
cout << "3.Multiplication" << endl;
cout << "4.Division" << endl;
cout << "5.Average Calculator" << endl;
cin >> whatoperation;
system("CLS");
switch(whatoperation){
case 1:
cout << "Enter in how many numbers you want to use" << endl;
cin >> Amount;
for(int i = 0; i < Amount; i++) // Keeps looping until i == Amount
{
cout << "Number " << i << ": " ; // Shows user what number they are entering
cin >> Number;
Total = Number + Total; // Adds number to total
}
cout << "The total is " << Total << endl; // Prints out total value when loop has finished
break;
case 2:
cout << "Enter in how many numbers you want to use" << endl;
cin >> Amount;
for(int i = 0; i < Amount; i++) // Keeps looping until i == Amount
{
cout << "Number " << i << ": " ;
cin >> Number;
if(i == 0)
{
Total = Total + Number;
}
else
{
Total = Total - Number;
}
}
cout << "The total is " << Total << endl; // Prints out total value when loop has finished
break;
case 3:
cout << "Enter in how many numbers you want to use" << endl;
cin >> Amount;
for(int i = 0; i < Amount; i++) // Keeps looping until i == Amount
{
cout << "Number " << i << ": " ;
cin >> Number;
if(i == 0)
{
Total = Total + Number;
}
else
{
Total = Total * Number;
}
}
cout << "The total is " << Total << endl; // Prints out total value when loop has finished
break;
case 4:
cout << "Enter in how many numbers you want to use" << endl;
cin >> Amount;
for(int i = 0; i < Amount; i++) // Keeps looping until i == Amount
{
cout << "Number " << i << ": " ;
cin >> Number;
if(i == 0)
{
Total = Total + Number;
}
else
{
Total = Total / Number;
}
cout << "The total is " << Total << endl; // Prints out total value when loop has finished
}
break;
case 5:
cout << "Enter how many numbers you want to find the average of: ";
cin >> Amount;
for(int i = 0; i < Amount; i++)
{
cout << "Number " << i << ": " ; // Shows user what number they are entering
cin >> Number;
Total = Number + Total; // Adds number to total
}
answertoaverage = Total / Amount;
cout << "The average of those " << Amount << " numbers is " << answertoaverage << endl;
}
cout << "enter 1 to restart or 2 to quit: ";
cin >> restart;
if(restart == 1){
goto loop;
}
system("pause");
return 0;
}
I just skimmed through it, I'm on my phone so I can't look to closely at it. But, first of all don't use system() for anything. It's not portable, it's slow, and it's a security risk. Second, you have a lot of repeating code. See if you can find it and then see what you can make functions of it. Functions are used so you don't have to repeat any code.
Another thing, you want a default case for your switch statement. And also, see of you can think of/find a way to handle input validation. What if instead of 1,2,3,4, or 5, I type in Q? Your program crashes. I know these might be kind of advanced topics for you currently, but they're not hard and it's something you should at least be thinking about.