Newbie to c++

Pages: 12
Hi guys. Im totally new to C++ and coding has never been straight forward to me.

I have commented out the brief I need to do. Below is the code I have so far, feel free to give me advice.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*Jason opened a coffee shop at the beach and sells coffee in three sizes : small(9oz), medium(12oz), and
large(15oz).The cost of one small cup is £1.75, one medium cup is £1.90, and one large cup is £2.00.
Write a menu - driven program that will make the coffee shop operational.Your program should allow
the user to do the following :
a.Buy coffee in any size and in any number of cups.
b.At any time show the total number of cups of each size sold.
c.At any time show the total amount of coffee sold.
d.At any time show the total money made.
Your program should consist of at least the following functions : a function to show the user how to use
the program, a function to sell coffee, a function to show the number of cups of each size sold, a
function to show the total money made.Your program should not use any global variables.*/


#include <iostream>

using namespace std;

// Function Prototypes
void instructionrerun();
void choosesize();

int main()
{
	//declare variables
	char Size;

	//welcome/Instructions on how to use the program.
	cout << "*-----------------------------------------------------------------*" << endl;
	cout << ":                       Jason Coffee Shop                         :" << endl;
	cout << ":                                                                 :" << endl;
	cout << "*-----------------------------------------------------------------*" << endl;

	// Call Function
	instructionrerun();
	
cout << "*-----------------------------------------------------------------*" << endl;
cout << ":                             Price:                              :" << endl;
cout << ":                 Size  : Small : Medium : Large                  :" << endl;
cout << ":                 Price : 1.75  : 1.90   : 2.00                   :" << endl;
cout << ":                                                                 :" << endl;
cout << "*-----------------------------------------------------------------*" << endl;

	choosesize();
				
// End Main Function 

system("PAUSE");
return 0;
}

void instructionrerun() {
	{
		
	
	};
	
		
	cout << "*-----------------------------------------------------------------*" << endl;
	cout << ":                          Instructions:                          :" << endl;
	cout << ":                         Select cup size                         :" << endl;
	cout << ":                      Select Number of cups                      :" << endl;
	cout << ":                                                                 :" << endl;
	cout << "*-----------------------------------------------------------------*" << endl;
}

void choosesize() {


	cout << "Cup Size\n\n";
	cout << "Small Cup = " << smallcoffee << endl;
	cout << fixed;
	cout.precision(2);
	cout << "Medium Cup = " << mediumcoffee << endl;
	cout << "Large Cup = " << largecoffee << endl;

	cout << "\n\nWould you like a Small (S), Medium (M), or Large (L) coffee? ";
	cin >> Size;
	


}
Last edited on
Well, here is some advice in your choosesize() function. You are using the variables: smallcoffee, mediumcoffee, and largecoffee, even though you never declared them. And the char Size was declared in a seperate function, so technically, it doesn't exist in the choosesize() function. One way to fix that is to use parameters.

When you prototype your choosesize() function at the top, add parameters in the parentheses, like so:
1
2
void choosesize(double smallCoffeePrice, double mediumCoffeePrice, 
double largeCoffeePrice);


Then, change the function at the bottom when you write it.


Now, in your main function, you will want to get some variables (3, to be exact.). Set the variables as the prices of each size coffee, like so:
1
2
3
double smallCoffee = 1.75;
double mediumCoffee = 1.90;
double largeCoffee = 2.00;


Now, when you use your chooseSize() function, incorporate those variables into the parameters, like this:
1
2
3
4
5
//in main() function

//the menu

choosesize(smallCoffee, mediumCoffee, largeCoffee);


That should work for you. Glad to help and good luck!
Thanks very much for your advice. I will try it out and do some more research into it :) Thanks !

@ this section im trying to print out the word 'Small, Medium, Large, ive tried "cout << "The size you choose is " << smallcoffee, mediumcoffee, largecoffee;" but that prints out the price and if i change the cout << "The size you choose is " << size; it only prints out s, m or l, how can I make it print out the whole word?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void choosesize(char& size, double smallcoffee, double mediumcoffee, double largecoffee) {

	cout << "Cup Size\n\n";
	cout << "Small Cup = " << smallcoffee << endl;
	cout << fixed;
	cout.precision(2);
	cout << "Medium Cup = " << mediumcoffee << endl;
	cout << "Large Cup = " << largecoffee << endl;

	cout << "\n\nWould you like a Small (S), Medium (M), or Large (L) coffee? ";
	cin >> size;
	cout << "The size you choose is " << smallcoffee, mediumcoffee, largecoffee;

	

}


Last edited on
Hi,

Just a quick tip:
Variables (no matter how descriptive their names like 'small', 'medium', or 'large') contain only what you put into them.

So, that means if you stored a character into 'size, and then tried to output it, you would only get the character stored in 'size', which might be an l, s, or m.

What you CAN do, is use 'if' statements (or a switch statement, but that's a bit more confusing) like this:

if(size == 'l')
{
cout<<"The size you choose is large"<<endl;
}
else if(size == 'm')
{
cout<<"The size you choose is medium"<<endl;
}
else if(size == 's')
{
cout<<"The size you choose is small"<<endl;
}

Note that there are better ways to handle this problem, but this one should work. :)

Joe
or use an enumerated type
http://www.cprogramming.com/tutorial/enum.html

something like this:
 
enum CoffeeSize {SMALL, MEDIUM, LARGE};

then using a switch statement would be a little easier and clearer to read.
Combination of enum and string array should help here. See below code for example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <string>

enum CoffeeSize_enum          { SMALL,   MEDIUM,   LARGE };
std::string CoffeeSize_str[]= {"SMALL", "MEDIUM", "LARGE"};

int main()
{
  char size_char;
  CoffeeSize_enum CoffeeSize;
  
  std::cout << "Enter coffee size (S or M or L): ";
  std::cin >> size_char;
  switch(size_char)
  {
    case 'S':
      CoffeeSize = SMALL;
      break;
    case 'M':
      CoffeeSize = MEDIUM;
      break;
    case 'L':
      CoffeeSize = LARGE;
      break;
    default:
      std::cout << "Incorrect input. Exiting" << std:: endl;
      return -1;
  };
  std::cout << "You choose: " << CoffeeSize_str[CoffeeSize] << " cup size coffee." << std::endl;
  return 0;
}
@kannanmj
Just note that for clarity, you should specify the indexes referred to by the enum indexes, and specify the data type of the enum (unsigned int). However, the easiest option would be to use a std::map:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <unordered_map>
#include <exception>
#include <string>

int main() {
    const std::unordered_map<char, std::string> CoffeeSizes = {
        {'S', "SMALL"},
        {'M', "MEDIUM"},
        {'L', "LARGE"}
    };

    try {
        char sz;
        std::cout << "Enter coffee size (S or M or L): ";
        std::cin >> sz;
        std::cout << "You chose: " << CoffeeSizes.at(sz) << " cup sized coffee.\n";
    } catch (const std::exception& e) {
        std::cerr << "Invalid coffee size entered.\n";
        return 1;
    }
    return 0;
}

http://ideone.com/Zbi7J5
ooo, so many ideas, great advice, thanks again :)
Ive added a while loop to select which output I want, but now im trying to do a validation to my program. So far I have come up with this for validation but whenever I type other letters it dosent print out 'you must enter 1, m or s'.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
void choosesize(char& size, double smallcoffee, double mediumcoffee, double largecoffee) {

	cout << "Cup Size\n\n";
	cout << "Small Cup = " << smallcoffee << endl;
	cout << fixed;
	cout.precision(2);
	cout << "Medium Cup = " << mediumcoffee << endl;
	cout << "Large Cup = " << largecoffee << endl;

	cout << "\n\nWould you like a Small (S), Medium (M), or Large (L) coffee?\n";
	cout << "choose size\n"; 
	//check user input and validates it
	while (!(cin >> size)) {
		cout << "You must enter l, m or s\n";
		cin.clear();
		cin.ignore();
	
		
	}

		if (size == 'l')
		{
			cout << "The size you choose is Large" << endl;
		}
		else if (size == 'm')
		{
			cout << "The size you choose is Medium" << endl;
		}
		else if (size == 's')
		{
			cout << "The size you choose is Small" << endl;
		}
Keep looping till "size" gets 'S', 'M', or 'L'. Sample code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <limits>

int main()
{
  char size;
  std::cout << std::endl;
  std::cout << "Would you like a Small (S), Medium (M), or Large (L) coffee? Choose the size: ";
  std::cin >> size;
  while(!(size == 'S' || size == 'M' || size == 'L'))
  {
    std::cout << std::endl;
    std::cout << "Enter S, M or L to choose Small, Medium or Large respectively: ";
    std::cin.clear();
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    std::cin >> size;
  }
  return 0;
}
Thanks you ! ive finally fixed it, took me forever how to figure it out. I didnt know about the while (!(cin >> size)) { part and you have to include cout while (!(size == 'S' || size == 'M' || size == 'L')){ the only input allowed. Once I got that I just had an infinite loop, but that was fixed using the cin.clear(); cin.ignore();
Last edited on
Im trying to figure out the the if statement to allow the user to enter the amount, then * it to the price to give the overall price. Would it be better to add another function or to do it all in one function?

I have tired coding around but I cant seem to get the logic right, I know my code below is pretty crap but any advice would be great

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
void buycoffee(char& size, double smallcoffee, double mediumcoffee, double largecoffee) {

	cout << "Cup Size\n\n";
	cout << "Small Cup = " << smallcoffee << endl;
	cout << fixed;
	cout.precision(2);
	cout << "Medium Cup = " << mediumcoffee << endl;
	cout << "Large Cup = " << largecoffee << endl;

	cout << "\n\nWould you like a Small (S), Medium (M), or Large (L) coffee?\n";
	cout << "choose size\n";
	//check user input and validates it, only S, M and L were allowed, rest are ignored.
	cin >> size;
	while (!(size == 'S' || size == 'M' || size == 'L')){
		cout << "Please select S, M or L" << endl;
		cin.clear();
		cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
		cin >> size;
	}

	if (size == 'L')
	{
		cout << "The size you choose is Large" << endl;
		cout << "\n\How many ?\n";
		//cin >> amount;
	}
	else if (size == 'M')
	{
		cout << "The size you choose is Medium" << endl;
		cout << "\n\How many ?\n";
		//cin >> amount;
	}
	else if (size == 'S')
	{
		cout << "The size you choose is Small" << endl;
		cout << "\n\How many ?\n";
		//cin >> amount;
	}

	}





Last edited on
regardless of the size you have to ask the amount. so ask on line 20 I reckon.

Incidentally, if you do all this using a class, then you could have as member variables the 3 different prices, meaning you wouldn't have to pass them into functions.
Last edited on
Finally I have got it right after countless tries :)
im trying to fix a user input validation. So ive added a cin.good to my while loop to Check whether state of stream is good or not, but its not working, it should go through this loop

1
2
3
4
5
6
7
8
cout << "The size you choose is Small" << endl;
		cout << "\n\How many ?\n";
		cin >> amount;
	}
	if (amount <= 0)
		cout << endl << "Thank you for your order.";
	else
	cout << "Would you like to add onto your order?\n";



code so far



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
cout << "Cup Size\n\n";
	cout << "Small Cup = " << smallcoffee << endl;
	cout << fixed;
	cout.precision(2);
	cout << "Medium Cup = " << mediumcoffee << endl;
	cout << "Large Cup = " << largecoffee << endl;

	cout << "\n\nWould you like a Small (S), Medium (M), or Large (L) coffee?\n";
	cout << "choose size\n";
	//check user input and validates it, only S, M and L were allowed, rest are ignored.
	cin >> size;
	while (!(cin.good) && !(size == 'S' || size == 'M' || size == 'L')){
		cout << "Please select S, M or L" << endl;
		cin.clear();
		cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
		cin >> size;
		}

	if (size == 'L')
	{
		cout << "The size you choose is Large" << endl;
		cout << "\n\How many ?\n";
		cin >> amount;
	}
	else if (size == 'M')
	{
		cout << "The size you choose is Medium" << endl;
		cout << "\n\How many ?\n";
		cin >> amount;
	}
	else if (size == 'S')
	{
		cout << "The size you choose is Small" << endl;
		cout << "\n\How many ?\n";
		cin >> amount;
	}
	if (amount <= 0)
		cout << endl << "Thank you for your order.";
	else
	cout << "Would you like to add onto your order?\n";

	}
Last edited on
Never mind fixed it.
Hi all again, Im trying to re-loop my program with an ('Y'/'N'). I have used the || compare the value operator, but how can I get it to only accecpt capital letter of Y/N? I have also looked into the creating mulitple cases but I dont really wanna change my code that much.

Thanks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
	if (amount <= 0)
			cout << endl << "Thank you for your order.";
		else 
			cout << "Would you like to add onto your order (Y/N)?\n";
		cin >> again;
		}
	    if (again == 'Y' || again == 'y')
		{
			cout << "Select";
		}
		else if (again == 'N' || again == 'n')
		{
			cout << "Thanks! Goodbye!\n";
		}
}
Last edited on

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

int main()
{
  char opt;
  std::cout << "File: " << __FILE__ << " Line: " << __LINE__ << std::endl;
  while(1)
  {
    std::cout << "Enter Y to continue looping. Press anyother key to exit: ";
    std::cin >> opt;
    if(opt == 'Y') continue;
    else break;
    }
  std::cout << "File: " << __FILE__ << " Line: " << __LINE__ << std::endl;
  return 0;
}

Cool fixed it :)
Pages: 12