Getline String Help

I am new with learning C++. I have some basic C experience and currently teaching myself how to program in C++. I have a senior project for college that I am learning it for. I am currently working on a section of code of the overall program.

What I would like the code to do is to take a letter key as the input and output a text line if the condition is met. I would like it to accept the key without having to hit enter or to accept multiple inputs. The code I have listed will only accept 'b' first to work properly. I just added the while loop to see what was going on. The code I am playing around with right now is:

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
/*This code is for different products and their dollar value.
  Version 1
*/

#include <iostream>
#include <string>

using namespace std;

int main()
{
	while(1)
	{
	string Beer;

	getline(cin, Beer, '\n');

	if(Beer == "b")
		{ 
		cout << "\n6 Pack of Budlight..........$6.49\n";
		}
	
	string Candy;

	getline(cin, Candy, '\n');

	if(Candy == "c")
		{ 
		cout << "\nSnickers Bar................$1.09\n";
		}
		
	}
	system("PAUSE");

	return 0;
} 


Pointers, advice, links to help direct me, etc would be greatly appreciated. I will plan on adding more products this is just to get me started.
Last edited on
I would like it to accept the key without having to hit enter or to accept multiple inputs
http://stackoverflow.com/questions/421860/c-c-capture-characters-from-standard-input-without-waiting-for-enter-to-be-pre
Thank you for your response. Not sure how I can use that. I tried to use one of the examples on the page and not sure how it worked.

I guess worse case scenario I could use the enter key.


Also more information:

1. I am using Microsoft Visual Studio to compile.
2. Project is in windows terminal
3. Need to be able to enter the input in any order. (with my progarm above it will only accept 'b' first to work.

I found the string portion of my program here: http://www.cprogramming.com/tutorial/string.html and modified the variable for my use. Since I am new to this I don't fullu understand how it works and why it needs to be that way. What I was working on before looked like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/*  This code is for different products and their dollar value.
  Version 1
*/

#include <iostream>

using namespace std;

int main()
{
	char Beer = 'b'; //declaring the product of beer
	char Candy = 'c'; //declaring the product of candy
	char Milk = 'm'; //declaring the product of milk
	char Rice = 'r'; //declaring the product of rice
	
	cout << "Select the product you desire to purchase: \n";
	cout << "Beer = b, Candy = c, Milk = m, and Rice = r.\n";
	cin >> Beer;
	
	if(b==1)
		cout << "6 pack of Budlight			$6.49";
		
	return 0;
}
Line 20 makes no sense. You have no variable called 'b'.

You need a single character (e.g. "char Product") and read it in, then check which button was used:

1
2
3
4
5
6
7
8
char Product;
cin >> Product;

switch(product) {
  case 'b': << cout "It's beer!"\n"; break;
  case ....
}
 



[edit]

That was at your last post. Your first post makes similar mistakes. Also, most importantly, don't use while(1). That is probably the most blatant example of an infinite loop. As there is nothing in your code that escapes the loop, it will continue forever.

Using the switch I explained above, you can easily add a "Quit" option (e.g. "case 'q'") that exits. For example, change your while to while (Product != 'q') { ... }
Last edited on
Thank you Gaminic. I thought about a switch case before, but didn't think it would work for my need. While testing it out I realized it could. I just need to put it in a while loop until the user is finished. I will test it out thoroughly when I get a chance.

The next problem I will see I need to over come is asking for all the inputs first then displaying the outputs.




[edit]

The while(1) was just to test it in a loop.

This is my up dated code using the switch/case:

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
/*  
  This code is for different products and their dollar value.
  Version 3
*/

#include <iostream>

using namespace std;

char test = 0;

int main()
{
	char product; //declaring the variable for the switch/case
		
	cout << "Select the product you desire to purchase: \n";//Asking user to input product purchasing
	cout << "For Beer press the b button.\n"; 
	cout << "For Candy press the c button.\n"; 
	cout << "For Milk press the m button.\n"; 
	cout << "For Rice press the r button.\n"; 
	cout << "When finished checking out press the z button.\n";
	
	while(test < 1)
	{
		cin >> product; //Taking input and assining to the variable product
		
		switch(product)
		{
			case 'b': cout << "6 pack of Budlight..........$6.49\n"; // If the button b is pushed displays
			break;
			case 'c': cout << "Snickers Bar.................$0.99\n";// If the button c is pusehd displays
			break;
			case 'm': cout << "1 Gallon of 2% Milk..........$3.99\n";//If the button m is pushed displays
			break;
			case 'r': cout << "Box of Brown Rice............$2.79\n";//If the button r is pushed displays
			break;
			case 'z': test++;
			break;
		}
	}
	
	system("pause");

	return 0;
}
Last edited on
Topic archived. No new replies allowed.