C++ add Y/N and make int accept numbers only

How can I confirm the int inputs are numbers? How can I add a Y/N to the end of the program to let the user confirm the order or start again.

// This program lets users buy shirts

#include <iostream>
#include <string>
#include <sstream>
#include <cstdlib>

using namespace std;
int main()

{
int a, c;
do
{
cout << " Coustom fit dress shirts";
cout << endl;
cout << "Enter the length of your arms and the circumfrence of your chest in centemeters." << endl;

cout << "arms ";
cin >> a;
cout << "chest ";
cin >> c;


cout << "You entered your Arm Length: " << a;
cout << endl;

cout << " Chest circumfrence: " << c;
cout << endl;


cout << "Shirts are $25.00 each plus tax";
cout << endl;

int count;
cout << "How many shirts would you like to buy?: ";
cin >> count;
cout << endl;

char confirm;
const int price = 25.00;
int subtotal = count*price;
int tax = subtotal*0.08;
int total = subtotal + tax;
cout << "Your total is: $"<< total;
cout << "Press 'Y' to confirm this order or 'N' to restart. ";
cin >> confirm;
while(confirm == 'n');

return 0;

}
Remember that you can edit your post and put all your comments and/or questions in a single post, rather than one post for each sentence.
How can I confirm the int inputs are numbers?

Unfortunately there is no waterproof solution, but this one should be working for you.

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
#include <iostream>
#include <string>
#include <exception>

using namespace std;

bool validInt(string& input)
{
  for (char ch : input)
  {
    if (!isdigit(ch))
    {
      return false;
    }
  }
  return true;  
}

int main()
{
  cout << "Enter number ";
  string input;
  cin >> input;
  if (validInt(input))
  {
    cout << "Valid";
  }
  else
  {
    cout << "Not valid";
  }
  return 0;
}


Output
1
2
3
4
5
Enter number 12345
Valid

Enter number 12sw34
Not valid
You should not use the variable "count" if you use "using namespace std;" because std::count is a function name. (http://www.cplusplus.com/reference/algorithm/count/)

This kind of misuse of reserved names can cause very "interesting" behavior in your program. To prevent problems it is therefor recommended to not use "using namespace std;" and just put std:: in front of the functions you from the standard library that you want to use.

Your program termination
 
while(confirm == 'n');

did not work because of two mistakes:
1. You did not close your brackets.
2. You declares the variable confirm inside the do-while loop, but the condition is not part of the inside of the loop and therefor the variable has to be declared before you start the loop.

For the number validation I would use:
1
2
3
4
5
6
7
8
9
10
11
12
bool is_a_number(std::string userInput, int& numberOfProducts)
{
    try
    {
        numberOfProducts = std::stoi(userInput); // if the string does not contain a number, the catch will be executed.
        return true;
    }
    catch(...)
    {
        return false;
    }
}

and below that in the main program:
1
2
3
4
5
6
7
8
        if (is_a_number(userInput, numberOfProducts))
        {
            const int price = 25.00;
            int subtotal = numberOfProducts*price;
            int tax = subtotal*0.08;
            int total = subtotal + tax;
            cout << "Your total is: $"<< total;
        }


Some other notes:
If this was real I have just sold you a lot of shirts and you own me a lot of money. Maybe you should consider only accepting positive numbers for order quantity :-p.

Also, I would personally use complete words for your variables a and c, this is a habit that is going to cost you a lot of time in the future.

And I would declare them as strings and apply the same validation on them. If you enter "short" for arms the current response of your program is "interesting".

I hope it helps you a bit.

Kind regards, Nico
Last edited on
closed account (48bpfSEw)
it is not enough to check if a string has only numbers!
An integer can only contain a range of numbers between (about) -32000 and +32000
Great advice! thank you! :)
@Nico,

I am afraid there is a problem with your solution.
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
#include <iostream>
#include <string>

bool is_a_number(std::string userInput, int& numberOfProducts)
{
    try
    {
        numberOfProducts = std::stoi(userInput); // if the string does not contain a number, the catch will be executed.
        return true;
    }
    catch(...)
    {
        return false;
    }
}

int main()
{
  int numProducts = 0;

  if (is_a_number("123gjhgdg12", numProducts))
    std::cout << "input correct " << numProducts;
  else
    std::cout << "Incorrect " << numProducts;

  return 0;
}


Output:
input correct 123
Thanks Thomas1965. I did not think to test a combined input, but you are right: I should have, I'll remember that one.

Kind regards, Nico
@Nico,

you are very welcome. I fell into a similar trap with the stringstream a while ago.

Remember to include the header <ctype> so that you can use tolower() and toupper()

I tried this and got

shirts.cpp:7:17: fatal error: ctype: No such file or directory

I am working on Ubuntu using Geany IDE if that matters.
the header should be <cctype> to use the tolower and toupper functions.

http://www.cplusplus.com/reference/cctype/?kw=cctype
Last edited on
Topic archived. No new replies allowed.