• 0

A couple C++ troubles


Question

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

  • 0

Ok, well a couple things I noticed.

1. Try to have a bit better indentation and variable names. It really is easier to understand.

2. You should have main() return an int. See below code insert.

3. You should add a line for using namespace std. I had to do it for my IDE, maybe yours is different though.

using namespace std;
int main()

4. The reason it repeats forever is because...

while(repeat = 1)  // INCORRECT!!!  don't use assignment operator

while(repeat == 1)  // CORRECT    use the equals to operator

5. Also, consider using <iostream> instead of <iostream.h>.

6. You should put numbers in front of the choice, if that's what you want. Currently, pressing C for chips does not do anything. Something like this...

If you want, you can use char, just make sure to ask for a char, not an int.

cout &lt;&lt; "   1: $1  S - Sandwich       " &lt;&lt; endl;
cout &lt;&lt; "   2: $0.75 C - Chips        " &lt;&lt; endl;
cout &lt;&lt; "   3: $0.35 P - Pickle        " &lt;&lt; endl;
cout &lt;&lt; "   4: $0.45 B - Brownie        " &lt;&lt; endl;
cout &lt;&lt; "   5: $0.75 R - Regular Drink      " &lt;&lt; endl;
cout &lt;&lt; "   6: $1.10 L - Large Drink       " &lt;&lt; endl;
cout &lt;&lt; "               " &lt;&lt; endl;
cout &lt;&lt; "     7: X - Cancel Sale and Start over      " &lt;&lt; endl;
cout &lt;&lt; "     8: T - Total the sale      " &lt;&lt; endl;

7. Upon further examination of the code, this line.. never reaches because repeat_number is never changed in the while loop.

if(repeat_number &gt; 1)
{
     cout &lt;&lt; "Your current Total is " &lt;&lt; current &lt;&lt; "." &lt;&lt; endl;
}

Hope that helps!

Edited by gflores
Link to comment
Share on other sites

  • 0

Dude, you;re using an asignation (repeat = 1) where a comparison need s to be used ((repeat == 1). The reason it's looping forever it's because instead of verifying IF the value is 1, it's instead assignning the variable repeat a value of 1, which is always going to be possible, and therefore, always will be truth.

Also, just a note, try note to use variable names with only one character, cause they make the code confusing and hard to follow. There might be more errors, but that's the first I noticed and the first one you should check.

Hope I was of assistance to you. :rolleyes:

Link to comment
Share on other sites

  • 0

using namespace std; <== actually, I supose you must be using an old C++ compiler, like borland 3.1, so this shouldn't be required

Also, the main function doesn't have to return anything, even though you have a return 0 statement near the end, I think the best way to go would be to just leave the main function as void, since there's no point in it returning anything (at least not for what you are doing)

Link to comment
Share on other sites

  • 0

Exactly, I don't need the main function to return anything. I am using Visual C++ 1.52, by the way.

That solved my mathematical problems for that program, thanks, but I'm still wondering how I can get it to always display 2 decimal places (for a monitary value)

Link to comment
Share on other sites

  • 0
using namespace std; <== actually, I supose you must be using an old C++ compiler, like borland 3.1, so this shouldn't be required

Also, the main function doesn't have to return anything, even though you  have a return 0 statement near the end, I think the best way to go would be to just leave the main function as void, since there's no point in it returning anything (at least not for what you are doing)

584783455[/snapback]

I'm not sure exactly when it's necessary. I'm using Dev-C++ 4.9 Beta (not new).

I think int main is the correct form according the ISO C++ Standard.

Link to comment
Share on other sites

  • 0
Also, the main function doesn't have to return anything, even though you  have a return 0 statement near the end, I think the best way to go would be to just leave the main function as void, since there's no point in it returning anything (at least not for what you are doing)

584783455[/snapback]

Incorrect. In C++, main can only be defined as returning int (if you write just "main() {", the int is implied). Anything else is a violation of the standard, and consequently incorrect. The return is not required, but if you leave it out, "return 0;" is implied and added automatically by the compiler.

So, the only valid declarations for main are:

int main(int argc, char *argv[])
int main()

Link to comment
Share on other sites

  • 0
I'm not sure exactly when it's necessary. I'm using Dev-C++ 4.9 Beta (not new).

I think  int main  is the correct form according the ISO C++ Standard.

584783500[/snapback]

I didn't even know there was a standard to it, so, it's good to know

If you want to display a certain amount of decimal places, please anybody correct me if I'm wrong, but you can't do it using cout. You could use the good and old printf(" Value is %5.2f ", 568521.2333333); where the first parameter is the string to print (note the %5.2f, this .2specifies the amount of decimal places), and the second one would be the number to print

Link to comment
Share on other sites

  • 0

That works for the total number of digits, not just the ones to the right of the decimal point.

Also, that just rounds to that number of digits, not carries it out if it has a less precise value.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.