The do loop of my program is not working as expected

Pages: 12
Hello,

here is a program that I wrote and run on visual studio 2008; after a compiled it I notice that some statements (cin.getline()) of the called function did not displayed in the second iteration of the d loop. I am not able to correct it so I need someone to help me please.


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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//This program consist of ,,,,, functions. It prompt the user to


#include <iostream>
#include <cstdlib>

using namespace std;

//prototype
void showIntro();
int getWinNumb();
int searchNumber(int [], int, int);


//Global variable.
const int SIZE = 10;
const int DIGIT = 6;

int main()
{
	//To hold the 5-digit lucky combination of the user.
	int luckyNumb[SIZE] = {13579, 26791, 26792, 33445, 55555, 62483, 77777, 79422, 85647, 93121};
	
	int winNumb;        //To hold the combination transformed into integer variable.       
	int results;        //To hold the result of the search function
	char repeat;        //To ask user to repeat the program or not.

	do 
	{
		//Displays introduction message.
		showIntro();
		
		//Get user 5-digits choice.
		winNumb = getWinNumb();

		//Search the winning number.
		results = searchNumber(luckyNumb, SIZE, winNumb);

		// If searchList returned -1, then 100 was not found.
		if (results == -1)
			cout << "You did not make the winning combination\n";
		
		else
		{
			// Otherwise results contains the subscript of
			// the first winNumb in the array.
			cout << "This is a winning combination number ";
			cout << (results + 1) << endl;
		}
		//Ask the user to repeat de program if necessary
		cout <<"Do you want to rerun the pregram?\n";
		cout <<"Enter Y or y for Yes or N or n for No\n";
		cin >> repeat;
	}while((repeat == 'y')||(repeat == 'y'));
		system("pause");
	return 0;
}
//*****************************************************************
// The getWinNumb function performs the string transformation     *
// into an integer variable. It accept a string variable as       *
// an argument and returns an interger.                           * 
//*****************************************************************
int getWinNumb()
{
	int numNumber;               //To hold a numeric data converted from a string.
	char strNumber[DIGIT];       //To hold the user's string input. 
	int length;                  //To hold the length of the input.
	
	//Get the user combination number.
	cout << "\nEnter your 5-digit Combination number" <<endl;
	cin.getline(strNumber, DIGIT);
	length = strlen(strNumber);

	//Length's input validation
	while(length!= 5)
	{
		cout <<"Error:You most Enter a 5-digit number\n";
		cout <<"Enter another Number\n";
  		cin.getline(strNumber, DIGIT);
		length = strlen(strNumber);
	}
	numNumber = atoi(strNumber);
	return numNumber;
}

//*****************************************************************
// The searchNumbers function performs a linear search on an      *
// integer array. The array list, which has a maximum of numElems *
// elements, is searched for the number stored in value. If the   *
// number is found, its array subscript is returned. Otherwise,   *
// -1 is returned indicating the value was not in the array.      *
//*****************************************************************

int searchNumber(int list[], int numElems, int combNumb)
{
   int index = 0;       // Used as a subscript to search array
   int position = -1;   // To record position of search value
   bool found = false;  // Flag to indicate if the value was found
   while (index < numElems && !found)
   {
      if (list[index] == combNumb)  // If the value is found
      {
         found = true;           // Set the flag
         position = index;       // Record the value's subscript
      }
      index++;                   // Go to the next element
   }
   return position;              // Return the position, or -1
}

//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*
// The function showIntro displays the introduction ^
//message of the program.                           ^
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*

void showIntro()

{

	cout << "\t##############################################################\n";
	cout << "\t# This program prompt a lottery player to enter his          #\n";
	cout << "\t# 5-digits combination number for the weekly lottery         #\n";
	cout << "\t# Then the program performs a search from the player's       #\n";
	cout << "\t# possible numbers and reports or not he mad the good choice.#\n";
	cout << "\t##############################################################\n";

}



Last edited on
code tags please. (I see you managed to find the bold tag.)
Last edited on
I did not understand you response. please can you be more clear?
Under "Format:" select the box which contains <>. Alternatively, you can just type the tags yourself. They would be code and /code with each having their own pair of brackets.
Last edited on
please I am a beginner on programming so I don't know where to find format. could you direct me plz!
When you're typing in your reply on this forum, look to the right of the box you're typing in. You'll see "Format:" and several buttons underneath. Click <>.

I just edited my message and I also did that.
Your source code must be within the "code" and "/code" tags though.
Text in the editor window looks like this:

[code]
for(auto i = foo.begin(); i != foo.end(); ++i)
{
...
}
[/code]

And in the post is rendered like this:

1
2
3
4
for(auto i = foo.begin(); i != foo.end(); ++i)
{
    ...
}


More info:
http://www.cplusplus.com/forum/articles/9044/

Last edited on
Ok I think I did fixed the issue with format not to confuse with the issue related to the program I posted.
please guys I need your help!!
Last edited on
You will find the answer in this article: http://www.cplusplus.com/forum/articles/6046/
Please I deathly need a solution for this problem I am facing can someone tell me what is wrong with the program I posted?
@PanGalactic

How did you escape the code tags so that they didn't make code?
The problem I see is that when the user inputs the same number of characters as the number that cin.getline() is reading, it will not go into the while loop, despite it being more than five characters. One fix to this would be to increase the amount of characters that cin.getline() is reading. Just make sure that you change the size of the array as well. You'll want the number to be an unlikely amount that the user would enter. Another solution would be to not use arrays.
Last edited on
@Anzo

I can't see anything wrong with this program. It works fine for me.

Output:
./test
##############################################################
# This program prompt a lottery player to enter his #
# 5-digits combination number for the weekly lottery #
# Then the program performs a search from the player's #
# possible numbers and reports or not he mad the good choice.#
##############################################################

Enter your 5-digit Combination number
12345
You did not make the winning combination
Do you want to rerun the pregram?
Enter Y or y for Yes or N or n for No
y
##############################################################
# This program prompt a lottery player to enter his #
# 5-digits combination number for the weekly lottery #
# Then the program performs a search from the player's #
# possible numbers and reports or not he mad the good choice.#
##############################################################

Enter your 5-digit Combination number
Error:You most Enter a 5-digit number
Enter another Number
43434
You did not make the winning combination
Do you want to rerun the pregram?
Enter Y or y for Yes or N or n for No
y
##############################################################
# This program prompt a lottery player to enter his #
# 5-digits combination number for the weekly lottery #
# Then the program performs a search from the player's #
# possible numbers and reports or not he mad the good choice.#
##############################################################

Enter your 5-digit Combination number
Error:You most Enter a 5-digit number
Enter another Number
34333
You did not make the winning combination
Do you want to rerun the pregram?
Enter Y or y for Yes or N or n for No
y
##############################################################
# This program prompt a lottery player to enter his #
# 5-digits combination number for the weekly lottery #
# Then the program performs a search from the player's #
# possible numbers and reports or not he mad the good choice.#
##############################################################

Enter your 5-digit Combination number
Error:You most Enter a 5-digit number
Enter another Number
55555
This is a winning combination number 5
Do you want to rerun the pregram?
Enter Y or y for Yes or N or n for No
n

Galik (104)

if you take a look on the output that you posted above, you will notice that after the first iteration, the message displayed on the screen were different from the previous one because the compiler had skipped the "cin.getline() function. from this part of the program
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//Get the user combination number.
	cout << "\nEnter your 5-digit Combination number" <<endl;
	cin.getline(strNumber, DIGIT);
	length = strlen(strNumber);

	//Length's input validation
	while(length!= 5)
	{
		cout <<"Error:You most Enter a 5-digit number\n";
		cout <<"Enter another Number\n";
  		cin.getline(strNumber, DIGIT);
		length = strlen(strNumber);
	}

the bold part of the code above is skipped( from cin.getline() ....to while(length!= 5))


These are the two messages:

first iteration:
Enter your 5-digit Combination number

second iteration:
Enter your 5-digit Combination number
Error:You most Enter a 5-digit number
Enter another Number.
Last edited on
cout <<"Error:You most Enter a 5-digit number\n";

There's a typo there. You have "most" instead of "must".

Anzo wrote:
if you take a look on the output that you posted above, you will notice that after the first iteration, the message displayed on the screen were different from the previous one because the compiler had skipped the "cin.getline() function.


I did not have that problem when I compiled and ran the program. The only issue I encountered was what I stated in my above post.
Okay, it seems that there is some whitespace still in the input buffer, probably the return key from the yes/no question.

There is the 'ws' manipulator to get rid of that:
1
2
3
4
5
	//Get the user combination number.
	cout << "\nEnter your 5-digit Combination number" <<endl;
	cin >> ws;
	cin.getline(strNumber, DIGIT);
	length = strlen(strNumber);


After adding that line the code works for me (it really does this time hehe)
Ah yes, the yes/no question takes only one character from the input, the 'Y' or the 'N', but not the '\n' you type to sent the input into the stream from the keyboard buffer.
@Galik: Embed an empty italic tag [i][/i] (or any other short tag) within the [code] tags so they looks like this: [co[i][/i]de]...[/co[i][/i]de]
Pages: 12