I'm a beginner trying to learn C++ by means of a tutorial that my mother bought me.
I'm trying to work my way through this tutorial here, but I keep running into problems.
This particular program should add up the price of items until the user decides to total it up and complete the sale. However, whenever the user enters information using this program that I wrote, it just repeats forever. (Note: I want to do this with a switch statement, because I could easily do it another way, but that's the lesson that I'm on in the tutorial.)
Could someone point out what I'm doing wrong?
//Calculates Total using a menu.
#include <iostream.h>
#include <math.h>
main()
{
//Define Variables
int s, c, p, b, r, l, x, t, repeat, dummy, i, repeat_number;
float sales_tax_mult, current, total;
float sprice, cprice, pprice, bprice, rprice, lprice;
?
s = 1;
c = 2;
? ?p = 3;
b = 4;
r = 5;
l = 6;
x = 7;
t = 8;
dummy = 0;
repeat = 0;
//Set prices
sprice = ?1;
cprice = .75;
pprice = .35;
bprice = .45;
rprice = .75;
lprice = 1.10;
//Set Sales Tax
sales_tax_mult = 1.065;
//Set values
current = 0;
total = 0;
repeat = 1;
repeat_number = 0;
//User info. output
while(repeat = 1)
{
repeat = 0;
?if(repeat_number > 1)
? {
? cout << "Your current Total is " << current << "." << endl;
? ? ? ? }
cout << "Welcome to Henry's snack bar." << endl;
cout << "Please begin your order by choosing a selection." << endl << endl;
cout << "*******************************************************" << endl;
cout << " ? ? ? ? ? ? ? " << endl;
cout << " ? $1 ?S - Sandwich ? ? ? " << endl;
cout << " ? $0.75 C - Chips ? ? ? ?" << endl;
cout << " ? $0.35 P - Pickle ? ? ? ?" << endl;
cout << " ? $0.45 B - Brownie ? ? ? ?" << endl;
cout << " ? $0.75 R - Regular Drink ? ? ?" << endl;
cout << " ? $1.10 L - Large Drink ? ? ? " << endl;
cout << " ? ? ? ? ? ? ? " << endl;
cout << " ? ? X - Cancel Sale and Start over ? ? ?" << endl;
cout << " ? ? T - Total the sale ? ? ?" << endl;
cout << " ? ? ? ? ? ? ? " << endl;
cout << " ? ? ? ? ? ? ? " << endl;
cout << "*******************************************************" << endl;
? ?cout << "Your choice? ";
? ?i = 0;
? ?cin >> i;
? ?switch(i)
? ? {
? ? case 1:
? ? ?current = current + sprice;
? ? ?repeat = 1;
? ? ?break;
? ? case 2:
? ? ?current = current + cprice;
? ? ?repeat = 1;
? ? ?break;
? ? case 3:
? ? ?current = current + pprice;
? ? ?repeat = 1;
? ? ?break;
? ? case 4:
? ? ?current = current + bprice;
? ? ?repeat = 1;
? ? ?break;
? ? case 5:
? ? ?current = current + rprice;
? ? ?repeat = 1;
? ? ?break;
? ? case 6:
? ? ?current = current + lprice;
? ? ?repeat = 1;
? ? ?break;
? ? case 7:
? ? ?current = 0;
? ? ?total = 0;
? ? ?repeat = 1;
? ? ?break;
? ? case 8:
? ? ?cout << "The total price is " << current << " without sales tax, and" << endl;
? ? ?total = (current * sales_tax_mult);
? ? ?cout << total << " including sales tax. ?Have a nice day.";
? ? ?break;
? ? }
? }
? ? ? ?
? ? ? ?
return 0;
}
Also, another prompt is asking me to write a program to simplify radicals. This is all I've gotten so far with it.
//Converts a square root to a multiplier and a square root
#include <iostream.h>
#include <math.h>
main()
{
//Define Variables
int n, i, rootpart, multiplyer;
//User Input
cout << "This program will simplify a square root." << endl;
cout << "Please input a square root." << endl;
cout << "sqrt(";
cin >> n;
cout << endl;
//Math/Loops
for(i = 1; (i * i) <= n; i++)
{
multiplyer = i;
rootpart = n % (i * i);
}
cout << "The result is " << multiplyer << " on the square root of " << rootpart;
return 0;
}
Question
HPMCommander
I'm a beginner trying to learn C++ by means of a tutorial that my mother bought me.
I'm trying to work my way through this tutorial here, but I keep running into problems.
This particular program should add up the price of items until the user decides to total it up and complete the sale. However, whenever the user enters information using this program that I wrote, it just repeats forever. (Note: I want to do this with a switch statement, because I could easily do it another way, but that's the lesson that I'm on in the tutorial.)
Could someone point out what I'm doing wrong?
//Calculates Total using a menu. #include <iostream.h> #include <math.h> main() { //Define Variables int s, c, p, b, r, l, x, t, repeat, dummy, i, repeat_number; float sales_tax_mult, current, total; float sprice, cprice, pprice, bprice, rprice, lprice; ? s = 1; c = 2; ? ?p = 3; b = 4; r = 5; l = 6; x = 7; t = 8; dummy = 0; repeat = 0; //Set prices sprice = ?1; cprice = .75; pprice = .35; bprice = .45; rprice = .75; lprice = 1.10; //Set Sales Tax sales_tax_mult = 1.065; //Set values current = 0; total = 0; repeat = 1; repeat_number = 0; //User info. output while(repeat = 1) { repeat = 0; ?if(repeat_number > 1) ? { ? cout << "Your current Total is " << current << "." << endl; ? ? ? ? } cout << "Welcome to Henry's snack bar." << endl; cout << "Please begin your order by choosing a selection." << endl << endl; cout << "*******************************************************" << endl; cout << " ? ? ? ? ? ? ? " << endl; cout << " ? $1 ?S - Sandwich ? ? ? " << endl; cout << " ? $0.75 C - Chips ? ? ? ?" << endl; cout << " ? $0.35 P - Pickle ? ? ? ?" << endl; cout << " ? $0.45 B - Brownie ? ? ? ?" << endl; cout << " ? $0.75 R - Regular Drink ? ? ?" << endl; cout << " ? $1.10 L - Large Drink ? ? ? " << endl; cout << " ? ? ? ? ? ? ? " << endl; cout << " ? ? X - Cancel Sale and Start over ? ? ?" << endl; cout << " ? ? T - Total the sale ? ? ?" << endl; cout << " ? ? ? ? ? ? ? " << endl; cout << " ? ? ? ? ? ? ? " << endl; cout << "*******************************************************" << endl; ? ?cout << "Your choice? "; ? ?i = 0; ? ?cin >> i; ? ?switch(i) ? ? { ? ? case 1: ? ? ?current = current + sprice; ? ? ?repeat = 1; ? ? ?break; ? ? case 2: ? ? ?current = current + cprice; ? ? ?repeat = 1; ? ? ?break; ? ? case 3: ? ? ?current = current + pprice; ? ? ?repeat = 1; ? ? ?break; ? ? case 4: ? ? ?current = current + bprice; ? ? ?repeat = 1; ? ? ?break; ? ? case 5: ? ? ?current = current + rprice; ? ? ?repeat = 1; ? ? ?break; ? ? case 6: ? ? ?current = current + lprice; ? ? ?repeat = 1; ? ? ?break; ? ? case 7: ? ? ?current = 0; ? ? ?total = 0; ? ? ?repeat = 1; ? ? ?break; ? ? case 8: ? ? ?cout << "The total price is " << current << " without sales tax, and" << endl; ? ? ?total = (current * sales_tax_mult); ? ? ?cout << total << " including sales tax. ?Have a nice day."; ? ? ?break; ? ? } ? } ? ? ? ? ? ? ? ? return 0; }Also, another prompt is asking me to write a program to simplify radicals. This is all I've gotten so far with it.
//Converts a square root to a multiplier and a square root #include <iostream.h> #include <math.h> main() { //Define Variables int n, i, rootpart, multiplyer; //User Input cout << "This program will simplify a square root." << endl; cout << "Please input a square root." << endl; cout << "sqrt("; cin >> n; cout << endl; //Math/Loops for(i = 1; (i * i) <= n; i++) { multiplyer = i; rootpart = n % (i * i); } cout << "The result is " << multiplyer << " on the square root of " << rootpart; return 0; }Link to comment
Share on other sites
13 answers to this question
Recommended Posts