-2

I am attempting at making a vending machine in c++. I am simply trying to add some validation to it so it wont break. I first ask the user to input the first two letters of their choice. I know i cant stop them from entering more then one char. I created a do while loop to make sure the first char and the second character aren't bigger then the maxChar. I am getting no syntactical error, but im not getting the correct answer. I know that a char is different then an int, but how would i convert the char to ints? any help would be greatly appreciated

#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <set>
#include <cctype>
#include <locale>


const int maxChr = 3;
char chrOne,chrTwo; 
   do
    { 
        cin >>chrOne>>chrTwo;
        if(chrOne + chrTwo > maxChr)
        {
            cout <<"you have too many characters"
            "please try again" << endl;
        } 
        while (chrOne + chrTwo > maxChr);  

    }
stallings
  • 5
  • 5
  • 6
    So... `'A' + 'B'` is definitely going to be larger than 3. – crashmstr Oct 29 '14 at 16:24
  • Are you summing up the **values** of the characters in order to determine **how many** are input by the user??? – barak manos Oct 29 '14 at 16:25
  • I'm confused. Do you mean to validate *how many* chars are entered or *which* chars are entered? – John Bollinger Oct 29 '14 at 16:26
  • Do you want to make the user enter 2 values? – Spikatrix Oct 29 '14 at 16:28
  • 1
    "im not getting the correct answer" - What answer do you get? What answer do you think should be correct? What is your input? – Mike Seymour Oct 29 '14 at 16:30
  • What does the vending machine vend exactly? – Neil Kirk Oct 29 '14 at 16:38
  • "I know i cant stop them from entering more then one char" is only true as long as you insist on using std::cin as there are other possibilities which offer more granular control over what's going on; maybe you like to have a look at _getch if you're on windows or continue [here](http://stackoverflow.com/questions/7469139/what-is-equivalent-to-getch-getche-in-linux). – wonko realtime Oct 29 '14 at 16:38
  • so lets say the person wants doritos. i ask for the first two letters 'd' and 'o' then i got to a switch statement. if the user enters lets say 'd' 'a' 'a' 'w' thats too many characters. i want check and make sure that there are only two characters. How can i do some arithmetic operation or verify charOne and charTwo dont exceed a total of two character – stallings Oct 29 '14 at 17:11
  • @stallings or of course [here](http://stackoverflow.com/questions/814975/getch-is-deprecated) as well – wonko realtime Oct 29 '14 at 17:11

4 Answers4

1

a do...while loop looks like:

do
{

} while ();

(your while is before the end bracket)

If you just want to get two chars (assuming you just want 0-9 as you asked for numbers in question):

#include <iostream>

int main()
{

  char in1,in2;
  do {
      std::cout << "please make a selection"
      cin.get(in1); 
      cin.get(in2);
      in1 -= '0'; //converts a char to the digit that represents it - needs checking though
      in2 -= '0';
      //at this point, you have grabbed both the inputs from the cmdline.
      //you'll need to ensure that these are valid.
  } while (!(in1 >= 0 && in1 <= 9 && in2 >= 0 && in2 <= 9)); //change as needed e.g. if you have a 5*6 machine, reduce the '9's to 5 and 6

  //you now have your input.
}
Baldrickk
  • 4,291
  • 1
  • 15
  • 27
0

The line if(chrOne + chrTwo > maxChr) does not check wether or not the user has inputted more than two characters, so according to what I understood you were saying this is wrong. If you want only one char you could user input in a string and make checks on this to see how many chars were put in by the user.

user10179
  • 53
  • 4
0

You are using

cin >>chrOne>>chrTwo;

Let's say user has entered more than two characters i.e absurd question.

Even then only first two characters would be stored on your variables i.e

chrOne='a'
chrTwo='b'

Please clarify what you are intending to do...

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
ravi
  • 10,994
  • 1
  • 18
  • 36
0

Okay i understand what you are trying to do but u are doing it a bad way... You will always read first two characters only!


This line chrOne + chrTwo is not what you are expecting to be. A in ASCII is the same as 65, B = 66 and so on. So in fact you are summing two numbers. 65+66 = 131 which is larger than 3;


I don't know if it is bad formatting here on StackOverflow but while(...) should come after }. This code shouldn't compile.

Quest
  • 2,764
  • 1
  • 22
  • 44