How do i make cout depend on cin

I'm a beginner at c++ programming, and i cant figure out how to change what cout is, depending on what the user of the program inputs. My program asks a yes or no question, and if the user inputs <const char string[80] = ("yes")> cout will say something different as opposed to if the user inputs <const char string[80] = ("no")>
a
Last edited on
closed account (D80DSL3A)
jjimijimmy wrote:
...i cant figure out how to...

Advice: Don't try to "figure out" how to do this stuff. Study how to do it. Read books or online tutorials. This will save you much pain.
closed account (S6k9GNh0)
joatmon, really...

jjimijimmy, http://social.msdn.microsoft.com/forums/en-US/vclanguage/thread/9f3b9646-5b2d-4ff0-88c8-bf72e56f12cd/
I think the OP is simply looking for some flow control.

Read the tutorials on this site and you will understand something like this:

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

int main()
  {
  cout << "Would you like some advice ('y' or 'n')? ";
  char c;
  cin >> c;
  cin.ignore( 500, '\n' );
  if (c == 'y')
    {
    cout << "Look before you leap.\n";
    }
  else
    {
    cout << "OK then. No advice for you!\n";
    }
  cout << "Have a nice day!\n";
  return 0;
  }

Hope this helps.
I am still new at this and it took me a few days to figure out how to make a conversion for coins. The cout is based on cin. Hope it helps.

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
#include <iostream>
using std::cout; // program uses cout
using std::cin; // proram uses cin
using std::endl; // program uses endl


int main() // function main begins program execution
{
	float money; // creates integer for money input
	int PENNY = 1; //creates integer for penny
        int DIME = 10; // creates integer for dime
	int QUARTER = 25; // creates integer for quarter
	int NICKEL = 5; // creates integer for nickel
	int total = 0; // creates integer for total and initializes it
	int choice = 0; // creates integer for choice and initializes it

	cout << "Please enter in a dollar amount to convert it to change\n"; //prompts user 
	>> cin >> money; // user enters dollar amount
	cout << endl; //end prompt
		
	cout << "To determine how many pennies, dimes, quarters or nickels, enter\n"; //display message
	cout << "MENU\n"
       << "1) PENNY\n" //user inputs 1 for penny conversion
       << "2) DIME\n" //user inputs 2 for dime conversion
       << "3) QUARTER\n" //user inputs 3 for quarter conversion
       << "4) NICKEL" << endl; //user inputs 4 for nickel conversion
	cin >> choice; //user enters number corresponding to coin
	cout << endl; //end prompt
	{
	if ( money <= 1 ) // if money input is 1 or less
	 return -1; // end program
		}
	{ // begin if...else
		if ( choice == 1 ) //if user chooses 1 for penny then
		 total = (int)( money * 100 ) / PENNY; // multiply money times 100 divided by 1 to get pennies
           else  
		     if ( choice == 2 ) //if user chooses 2 for dime then
              total = (int)( money * 100 ) / DIME; // multiply money times 100 divide by 10 to get dimes 
                else 
			     if ( choice == 3 ) //if user chooses 3 for quarter then
	               total = (int)( money * 100 ) / QUARTER; //multiply money times 100 divide by 25 for quarters
	                 else  
				       if ( choice == 4 ) //if user chooses 4 for nickel
	                     total = (int)( money * 100 ) / NICKEL; //multiply money times 100 divide by 5 for nickels
             
		
	if ( choice == 1 ) //if user choice was 1 display conversion and message
		cout << "The result is: " << total << " PENNIES in " << "$" << money << endl; // display message with amount of coins and money input
	else 
	if ( choice == 2 ) //if user choice was 2 display conversion and message
		cout << "The result is: " << total << " DIMES in " << "$" << money << endl; // display message with amount of coins and money input
	else 
	if (choice == 3 ) //if user choice was 3 display conversion and message
		cout << "The result is: " << total << " QUARTERS in " << "$" << money << endl; // display message with amount of coins and money input
	else 
	if ( choice == 4 ) //if user choice was 4 display conversion and message
		cout << "The result is: " << total << " NICKELS in " << "$" << money << endl; // display message with amount of coins and money input
} // end if..else

system("pause"); // keep window open
return 0; // indicates successful termination
}
I worked on your question today to help you and teach myself. So this is what I came up with.

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
#include <iostream> 
using namespace std; // uses std namespace

int main() // function main begins program execution
{  
	char choice = 0; // creates character for choice
	char y = 0; // creates character for y
	char n = 0; // creates character for n
	
	  
		cout << "Would you like some advice?\n"; // display question to user
		cout << "Y for yes and N for no\n"; // prompts user for a Y or N
		cin >> choice; // user enters in letter
	
  		
  switch ( choice ) // switch statement to determine what input letter and what to display based on input
  {
	case 'y': // lower case choice for y
	case 'Y': // upper case choice for Y
		cout << "\nLook before you leap.\n"; // message displayed if user inputs Y or y
		break; // exit switch
	case 'n': // lower case choice for n
	case 'N': // upper case choice for N
		cout << "OK then. No advice for you!\n";  // message displayed if user inputs N or n
		break; //exit switch
	default: // call all other characters
	    if ( choice != y || n ) // if the choice is not a y or n display below message
	cout << "Sorry only Y or N is allowed.\n"; //message displayed if condition is not met
	cout << endl; //output a newline

  } // end switch   
	  cout << "Have a nice day!\n"; //display message outputted with each choice
  cout << endl; //output a newline
 
system("pause"); // keeps window open, best used for learning codes, poses security vulnerability
  return 0; // indicate successful termination
  } // end function main 


You can negate the need for 'y' and 'Y' by using:
switch (tolower(choice))

Or:

if (tolower(choice) == 'y')
stuff
if (tolower(choice) == 'n')
stuff
else
stuff

A few less lines of code but either way is good.

instead of system pause its usually better to simply have a

cout << "Pause:\n" << "Enter to exit";
cin << new e;
delete e;
return 0;

Or the like.
:)
Last edited on
Topic archived. No new replies allowed.