pancake program problems

hi ,, i ve been doing c++ for a lil while and working on a practice program
,, i looked up and read information and tutorials on input validation,
i dont know if im just not understanding it correctly or what ,, im sure somewhere im doin something wrong that im not understanding , if any one could help or point me in the right direction , it would much appreciated

no problems with the other fucntions ,, they have yet to be written but i have no issue with them

please and thank you

/// programmer : alex gibbs
/// program : Pancake glutton
/// purpose : program takes number of pancakes 10 different people ate ,, tell who ate the most
/// , the least, nd displays list from least to great pancakes eaten

/// Write a program that asks the user to enter the number of pancakes eaten for breakfast by 10 different people
/// (Person 1, Person 2, ..., Person 10) Once the data has been entered the program must analyze the data
/// and output which person ate the most pancakes for breakfast.

/// Modify the program so that it also outputs which person ate the least number of pancakes for breakfast.

/// Modify the program so that it outputs a list in order of number of pancakes eaten of all 10 people.

#include <iostream>
#include <limits>
#include <cmath>

void bodyMain();
void sorting();
void display();
void mostEaten();
void leastEaten();

using namespace std;

int main()
{
bodyMain();
}

void bodyMain()
{
int person[9], eaten = 0;
string names[] = {"alex","james", "jesse" , "sarah", "paige", "pam", "lilith", "alicia", "richard"};

cout << "\n\n\tHello darlin, you got ten people right\n\t";
/// just gos straiaght to the else if statement ,, not sure why
/// tried it with entering valid input first ,, still doesnt work
for(int i = 0; i < 9;)
{
cout << "K, so " << names[i] << " how many did you eat?\n\t";
cin >> eaten;
person[i] = eaten;
if(isdigit(person[i]))
{
/// test input
cout << "person index of i " << person[i];
cout << "/nvalue of eaten " << eaten;
cout << "input valid";
}
else if(!(isdigit(person[i])))
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "input failed";
i--;
}
i++; ///added to code ,, forgot it when first posted
}
}
/// took out scrap code ,,


Last edited on
in my for loop ,, it just keeps going to the else if statement ,, tried rewriting it a lot of different ways ,, just not figuring out what im doin wrong with it .
isdigit tests to see if a character is among the characters '0', '1',...,'9'.
person is already an array of integers, so you've no need to test to see if each element is a number. Calling isdigit with an integer argument 0-9 will return false, which is why you'd enter the invalid input check.

But you might want to try something like this:
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
int main()
{
  int person[9]{ 0 }; //Initialize your array :D
  int eaten{ 0 };

  std::string names[] = { "alex","james", "jesse" , "sarah", "paige", "pam", "lilith", "alicia", "richard" };

  for (int i = 0; i < 9; i++)
  {
    std::cout << "K, so " << names[i] << " how many did you eat?\n\t";

    std::cin >> eaten;

    while (!std::cin)
    {
      std::cin.clear();
      std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
      std::cout << "Invalid input. Try again: "; 
      std::cin >> eaten;
    }

    //Successful input
    person[i] = eaten;
    std::cout << "person of index i " << person[i];
    std::cout << "\nvalue of eaten " << eaten;
    std::cout << "Input valid" << std::endl;
}

  for (int i = 0; i < 9; i++)
  {
    std::cout << names[i] << " ate " << person[i] << std::endl;
  }
}


That certainly is not pedantic with error checking, but it will hopefully steer you in the right direction.
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
63
64
65
66
67
68
/// programmer : alex gibbs
/// program : Pancake glutton
/// purpose : program takes number of pancakes 10 different people ate ,, tell who ate the most
/// , the least, nd displays list from least to great pancakes eaten

/// Write a program that asks the user to enter the number of pancakes eaten for breakfast by 10 different people
/// (Person 1, Person 2, ..., Person 10) Once the data has been entered the program must analyze the data
/// and output which person ate the most pancakes for breakfast.

/// Modify the program so that it also outputs which person ate the least number of pancakes for breakfast.

/// Modify the program so that it outputs a list in order of number of pancakes eaten of all 10 people.

#include <iostream>
#include <limits>
#include <cmath>

void bodyMain();
void sorting();
void display();
void mostEaten();
void leastEaten();

using namespace std;

int main()
{
    bodyMain();
}

void bodyMain()
{
    int person[9], eaten = 0;
    string names[] = {"alex","james", "jesse" , "sarah", "paige", "pam", "lilith", "alicia", "richard"};

    cout << "\n\n\tHello darlin, you got ten people right\n\t";
/// just gos straiaght to the else if statement ,, not sure why
/// tried it with entering valid input first ,, still doesnt work
    for(int i = 0; i < 9;)
    {
        cout << "K, so " << names[i] << " how many did you eat?\n\t";
        cin >> eaten;
        person[i] = eaten;
        if(isdigit(person[i]))
        {
/// test input
            cout << "person index of i " << person[i];
            cout << "/nvalue of eaten " << eaten;
            cout << "input valid";
        }
        else if(!(isdigit(person[i])))
        {
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
            cout << "input failed";
            i--;
        }
    }

///inputs the number of pancakes eaten for each person...
    for(int i = 0; i > 10;)
    {
        person[i] = eaten;
        i++;
        cout << "iteration " << i << "\n";
    }

}


Maybe it would help if you included your other function, I also wonder why you have most of the code in a function that is called by main() [bodyMain()].

here are some errors/improvements I found:
--On line 48 the newline sequence "\n" is incorrect (you have it as "/n")
--The body of your else if is unnecessary (You could have just outputed "input failed" and exited the program or retried the user's input)
--On line 39 your for() loop is missing the statement to advance i [ You have for(int i = 0; i < 9;), it should be for(int i = 0; i < 9; i++)]
--On line 51 you used an else if, but you could have just used a regular if statement
--On line 56 i--; will not work you will have to use some other way of retrying the user's input
--On line 60 your for() loop is again messed up again [You have for(int i = 0; i > 10;), it should be for(int i = 0; i < 10;i++), and also i++; on line 64 will not work! ]
--The comment on line 60 does not match the code below and is confusing
--The code on lines 63, will reset the amount of 'pancakes' each person ate to what the last user how many 'ate'
--Line 64: i++; does not work!
--Line 65 does not fit with the rest of the code in the for() loop it is in (was that code 'scrap'?)

Also where is the sorting(), display(), mostEaten(), and leastEaten() functions, for your program these are Crucial!

Here is the semi-unfinished-corrected code:
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
63
64
65
66
67
68
69
70
71
72
73
74
/// programmer : alex gibbs
/// edited by : popa6200, cplusplus.com forums
/// program : Pancake glutton
/// purpose : program takes number of pancakes 10 different people ate ,, tell who ate the most
/// , the least, nd displays list from least to great pancakes eaten

/// Write a program that asks the user to enter the number of pancakes eaten for breakfast by 10 different people
/// (Person 1, Person 2, ..., Person 10) Once the data has been entered the program must analyze the data
/// and output which person ate the most pancakes for breakfast.

/// Modify the program so that it also outputs which person ate the least number of pancakes for breakfast.

/// Modify the program so that it outputs a list in order of number of pancakes eaten of all 10 people.

#include <iostream>
#include <limits>
#include <cmath>

void bodyMain();
void sorting();
void display();
void mostEaten();
void leastEaten();

using namespace std;

int main()
{
    bodyMain();
}

void bodyMain()
{
    int person[9], eaten = 0;
    string names[9] = {"alex","james", "jesse" , "sarah", "paige", "pam", "lilith", "alicia", "richard"};

    cout << "Hello darlin, you got ten people right";
    // just gos straiaght to the else if statement ,, not sure why
    // tried it with entering valid input first ,, still doesnt work
    for(int i = 0; i < 9;)
    {
        cout << "OK, so " << names[i] << ".How many did you eat?\n";
        cin >> eaten;
        person[i] = eaten;
        if(isdigit(person[i]))
        {
            //tests input
            cout << "person index of i: " << person[i] << endl;
            cout << "value of eaten: " << eaten << endl;
            cout << "input valid" << endl;
        }
        /*else*/ if(!(isdigit(person[i])))
        {
            //Needs to be changed!
            /*
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
            cout << "input failed";
            i--;
            */
        }
    }
    //Scrap Code?
    /* 
    ///inputs the number of pancakes eaten for each person...
    for(int i = 0; i > 10;)
    {
        person[i] = eaten;
        i++;
        cout << "iteration " << i << "\n";
    }
    */

}


Good Luck, You'll need it.
Popa
popa6200 wrote:
--The body of your else if is unnecessary (You could have just outputed "input failed" and exited the program or retried the user's input)

The body of that is necessary. The user will be unable to attempt to enter input again if the fail state is not cleared and the invalid input discarded from the stream. It will simply continue to fail.
Though, a loop would best be used, since the user can just keep giving invalid input. Or incrementing i only in the body of the if-statement that was entered for valid input.

popa6200 wrote:
Also where is the sorting(), display(), mostEaten(), and leastEaten() functions, for your program these are Crucial!

For the problem OP has, the definition of those functions are useless right now. Including them would only add more code to filter through.
forgot to mention that the other functions have yet to be written yet ,, i fixed it in the post and also took out the scrap code the keep further confusion from happening,, also added the i++ increment that i forgot earlier

jayhawkzombie -- from what i ve read and understood isidigit returns true if its a digit other than zero 1- 9,, and id just make a special condition for zero or try too ,, something like char variable for them to type n or N for no
http://www.cplusplus.com/reference/cctype/isdigit/

popa -- for the for loop you dont have to have the increent in the for loop they are ok with out it , it might be sloppy code but its code that works ,

on line 56 , it works now since i ve added the i++ ,, i might have it in the wrong place though -- i remember while writing it that for some reason either out side the if or in (cant remember which) it just created an infinite loop

the scrap code was from earlier and just forgot to take it out ,, my apologies, i should have proofread the code a lil better before posting it ,,
I mean that if you send an integer with a value 0-9 (or anything not between 48 and 57, inclusive).
isdigit is used to check if the first character in str is a digit and therefore a valid candidate to be converted by atoi into an integer value.

It uses the ASCII values for characters to determine if a given character is a decimal digit (the value of that character when casted to an integer will be 48-57): http://www.asciitable.com/

But that person array is already an array of integers. You don't need to test to see if the element in that array is an integer, it can't hold anything but integers.

If the user tries to enter a character and cin is expected to get an integer, cin will fail, and subsequent calls to it will fail unless you clear the fail state.

You've got the right idea, but your if statements should be changed, because you're passing an integer to isdigit when that is meant for testing characters (if you were to pass 48-57, though, it would return true).
You can check for that cin failure instead of checking for isdigit.
ok kewl beans ,, thanks much appreciated!
ended up with this which works perfectly ,, thanks a lot for the help ,, i understand it a lot better now




for(int i = 0; i < 9;)
{
cout << "K, so " << names[i] << " how many did you eat?\n\t";
cin >> eaten;
person[i] = eaten;
if(cin)
{
/// test input
cout << "person index of " << i << " " << names[i];
cout << "\nvalue of eaten " << eaten;
cout << "\ninput valid";
}

while(!cin)
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "input failed";
i--;
}
i++;
}
}
I would put person[i] = eaten somewhere after you've verified the input was valid. In your code, the user could enter invalid input and your code will still try to assign eaten to person[i]

You also might not want to make i smaller in the while(!cin)... block. i goes down by 1 each time, but you're still on the same loop iteration in the for loop. Just leave i and keep asking them for valid input. If they keep entering bad input, i could even go below 0, and your computer isn't going to be happy if you try to write out of bounds.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
for (int i = 0; i < 9; i++)
{
  cout << names[i] << ", how much did you eat?: ";
  cin >> eaten;

  while (!cin)
  {
    cin.clear();
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    cout << "Invalid input. Try again: ";
    cin >> eaten;
  }

  person[i] = eaten;
  cout << "Person index: " << i << " " << names[i];
  cout << "\nValue of eaten: " << eaten;
  cout << "\nValue of person[i]: " << person[i];
  cout << "\nInput valid";
}
yea im was just testing it to make sure it worked right ,, ,, but i wanted the i-- for invalid input so it wouldnt skip a person and they would be able to try again ,, i++ outside of the while loop insures that even if the i - 1 it still go bak up one after the while loop is done
Topic archived. No new replies allowed.