I am working on a program that will have the user input two numbers ....the program then adds them together and prints out the sum.....the program is to then ask if you want to continue...the user is to type in y or n....how do i go about having the program read in the characters and continue the loop

Here's something to play with as a template:

#include <ios>
#include <iostream>
#include <limits>
#include <string>

using namespace std;

int main()
{
  bool done = false;

  do {
    int value;

    cout<<"Enter a number: ";

    if ( !( cin>> value ) )
      break;

    cout<<"You entered "<< value <<'\n';

    // Clean up leftover garbage
    cin.ignore ( numeric_limits<streamsize>::max(), '\n' );

    string option;

    cout<<"Continue? (y/n): ";

    if ( getline ( cin, option ) && option != "y" )
      done = true;
  } while ( !done );
}

ok well yea how about something a lil bit easier I will post my code that i have right now

// Bric Browning
//ASCII Adder


#include <stdio.h>

void math()
{

int num;
int x;
int sum;


printf("=========\n");
printf("Math Help\n");
printf("=========\n");

printf("Input first digit: ");
 scanf("%d", &num);
 
printf("Input second digit: ");
 scanf("%d", &x);

 sum = num + x;
printf("The Sum is %d\n", sum);

}




int main ()
{

int answer;
int y=0;
int n=0;

  math();
  
  
while(answer)
{  
printf("Continue? ");
scanf("%d", answer);


answer = toupper(answer);
if(answer == y)
{
 math();
}
 else if(answer == n)
 {
  return 0;
 }
 else 
 {
   printf("Please press Y or N");
 }
}


return 0;
}

as far as i know we are suppose to do it like this i have no clue if and or when we are gunna use c++ but if u can gimme a code that works or some help on this one i would appreciate it

ok well yea how about something a lil bit easier I will post my code that i have right now

// Bric Browning
//ASCII Adder


#include <stdio.h>

void math()
{

int num;
int x;
int sum;


printf("=========\n");
printf("Math Help\n");
printf("=========\n");

printf("Input first digit: ");
scanf("%d", &num);

printf("Input second digit: ");
scanf("%d", &x);

sum = num + x;
printf("The Sum is %d\n", sum);

}


int main ()
{

int answer;
int y=0;
int n=0;

math();


while(answer)
{
printf("Continue? ");
scanf("%d", answer);


answer = toupper(answer);
if(answer == y)
{
math();
}
else if(answer == n)
{
return 0;
}
else
{
printf("Please press Y or N");
}
}


return 0;
}

Code tags please:

[code]

// paste code here

[/code]

You're using C++, so might as well change the printf and scanf to cin and cout . Also, you can use bool in C++, so you might want to make answer a bool instead of an int . On the other hand, what exactly does answer represent?

int answer;  // integer
while (answer)  // suggests use as a boolean flag,
                       // but not initialized
answer = toupper(answer);  // suggests char
if(answer == y)   // int? char?  Where are the quotes?
              // what is the user supposed to enter?

You have these lines earlier:

int y = 0;
int n = 0;

>ok well yea how about something a lil bit easier
I like how you ask for something easier then post code of equivalent complexity.

Wait all he needs is a program that adds two numbers that are entered, then asks the user to continue yes or no? if yes then go back to main, if no then exit program, right?

#include <iostream>
#include <math.h>

using namespace std;

int main()
{
    char ExitOr;
    float FirstNumber;
    float SecondNumber;
    float TheAnswer;

    cout << "\nPlease enter the First Number\t";
    cin >> FirstNumber;
    cout << "\nPlease enter the Second Number\t";
    cin >> SecondNumber;

    TheAnswer = FirstNumber + SecondNumber;

    cout << "\nYour answer is " << TheAnswer << "!";
    cout << "\nThanks for using the program!";
    cout << "\nDo you want to run this program again? Y/N:\t";
    cin >> ExitOr;
    if (ExitOr == 'n')
    {
        exit(1);
    }
    else
    {
        if (ExitOr == 'y')
        {
            main();
        }
    }
    return 0;
}

That should work...no?
Unless by print he means it prints to a printer the result...

This article has been dead for over six months. Start a new discussion instead.