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:
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:
/* This code is for different products and their dollar value.
Version 1
*/
#include <iostream>
usingnamespace 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;
}
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') { ... }
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.
/*
This code is for different products and their dollar value.
Version 3
*/
#include <iostream>
usingnamespace 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;
}