//By Oscar Castillo//Date: 10/21/2012#include <iostream>#include <string>usingnamespace std;
int main ()
{
int multiply;
int add;
int n, sum = 0, count = 0;
cout<<"Would you like to add or multiply? ";
string choice;
getline(cin, choice);
while(choice != "add" && choice != "multiply")
{
cout<< "Sorry, I don't know how to " << choice <<endl;
cout<<"Would you like to add or multiply? ";
getline(cin, choice);
}
cout<<"Enter one number per line, 0 to stop : "<<endl;
if(choice == "add")
{
int accum = 0;
cin >> n;
while( n > 0 )
{
accum += n;
cin >> n;
}
cout << "The sum is " << accum << endl;
}
else// multiply
{
int accum = 1;
cin >> n;
while( n > 0 )
{
accum *= n;
cin >> n;
}
cout << "The product is " << accum << endl;
}
return0;
}
//the problem with this program is that i couldn't figure out how to stop it at 0 and that the addition and multiplicaion //output the wrong answer.
#include <iostream>usingnamespace std;
int main()
{
string choice;
while(true)
{
cout << "Would you like to add (enter 'a') or multiply (enter 'm')? ";
getline(cin, choice);
if(choice == "a")
{
cout << "Enter one number per line, 0 to stop:";
int num;
int acc = 0;
while(num != 0)
{
cin >> num;
acc = acc + num;
}
cout << "Thank You. The sum is " << acc << ".";
break;
}
if(choice == "m")
{
cout << "Enter one number per line, 1 to stop:";
int num;
int acc = 1;
while(num != 1)
{
cin >> num;
acc = num * acc;
}
cout << "Thank You. The product is " << acc << ".";
break;
}
// At this point, they typed something wrong.// Condition of neither a nor m is redundant.
cout << "Sorry. I don't know how to " << choice << ".\n";
}
return0;
}
//By Oscar Castillo//Date: 10/21/2012#include <iostream>#include <string>usingnamespace std;
int main ()
{
int multiply;
int add;
int n, sum = 0, count = 0;
cout<<"Would you like to add or multiply? ";
string choice;
getline(cin, choice);
while(choice != "add" && choice != "multiply")
{
cout<< "Sorry, I don't know how to " << choice <<endl;
cout<<"Would you like to add or multiply? ";
getline(cin, choice);
}
cout<<"Enter one number per line, 0 to stop : "<<endl;
int accum;
if(choice == "add") accum = 0;
else accum = 1;
cin >> n;
while( n > 0 )
{
if(choice == "add") accum += n;
else accum *= n;
cin >> n;
}
string word;
if(choice == "add") word = "sum";
else word = "product";
cout << "The " << word << " is " << accum << endl;
return0;
}
//the problem with this program is that i couldn't figure out how to stop it at 0 and that the addition and multiplicaion //output the wrong answer.