Char Array Help

I am working on this assignment for class and have been searching and can't seem to find a solution. I am having a problem after my program runs through one time and you type 'y' to start over, the first phrase automatically becomes "". I am unable to input a new phrase except for the second phrase. I am not seeing where the problem is or if I am completely missing something.

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
#include <iostream>
using namespace std;

int main()
{
	char phraseOne[80];
	char phraseTwo[80];
	int wordCount1;
	int wordCount2;
	int len1;
	int len2;
	int compare;
	char yOrN = 'Y';

	while(toupper(yOrN) == 'Y')
	{
		//Initialize each time through
		wordCount1 = 0;
		wordCount2 = 0;
		len1 = 0;
		len2 = 0;

		//Get phrase one
		cout << "Enter a phrase: ";
		cin.getline(phraseOne, 80);
		cout << endl << endl;

		//Get phrae two
		cout << "Enter another phrase: ";
		cin.getline(phraseTwo, 80);
		cout << endl << endl;

		//Count the number of words
		for(int i = 0; i < 80; i++)
		{
			if (phraseOne[i] == ' ')
				wordCount1++;
			if (phraseTwo[i] == ' ')
				wordCount2++;
		}

		//Find out the number of characters
		len1 = strlen(phraseOne);
		len2 = strlen(phraseTwo);

		//compare the two phrases
		compare = strcmp(phraseOne, phraseTwo);

		//Output the number of words and character of each phrase
		cout << "Phrase \"" << phraseOne << "\" has " << wordCount1 + 1 << " words and " << len1 << " characters." << endl << endl;
		cout << "Phrase \"" << phraseTwo << "\" has " << wordCount2 + 1 << " words and " << len2 << " characters." << endl << endl;

		//Output the results of the comparison
		if (compare > 0)
			cout << "In C++, phrase \"" << phraseOne << "\" is greater than phrase \"" << phraseTwo << "\"";
		else if (compare < 0)
			cout << "In C++, phrase \"" << phraseTwo << "\" is greater than phrase \"" << phraseOne << "\"";
		else
			cout << "In C++, phrase \"" << phraseOne << "\" is equal to phrase \"" << phraseTwo << "\"";

		//Ask the user to play again
		cout << "\n\nPlay again? y/n: ";
		cin >> yOrN;
		cout << endl;

		//Check for valid input
		while (toupper(yOrN) != 'Y' && toupper(yOrN) != 'N')
		{
			cout << "\nInvalid input, Play again? y/n: ";
			cin >> yOrN;
			cout << endl;
		}
	}

	//End the program
	cout << "\n\nThank you for using the phrase comparison program";
	sytem("pause")
	return 0;
}
Multiple suggestion, not sure if it'll help...

Use strings instead of vectors as you can then use the .size() in your for loops because trying to find a vector/array field that doesn't exhist gives errors,
for(int i = 0; i < phraseOne.size(); i++)

Also try using Cin as an aurgument in getline cos this just works for me quite well...
getline(cin, phraseOne);

Hope some of this may be helpful!h
@SunnyMcduff I cant help cause you wont be learning but I can guide you.

//Get phrase one
cout << "Enter a phrase: ";
cin.getline(phraseOne, 80);
cout << endl << endl;

//Get phrae two
cout << "Enter another phrase: ";
cin.getline(phraseTwo, 80);
cout << endl << endl;

Why do not you put this in a (if statement or while loop(that contains a if)). make your life easier so if the user say "Y" it will go back to the first phrase and if "N" it will quit program. delete the while loop and include that within the if. I hope this could help. Good Luck.
Last edited on
Why do not you put this in a (if statement) make your life easier so if the user say "Y" it will go back to the first phrase and if "N" it will quit program. I hope this could help. Good Luck.


My loop sends it back to the first phrase, the problem is that for some reason it keeps skipping over the prompt for the user to type in a value of the first phrase again. As though there is a value in the input stream waiting to be assigned automatically. I am able to type in an input for the second phrase every time. Here is what the program looks like the second time around.

Enter Phrase: Hello

Enter another Phrase: Hello Again

phrase "hello" has 1 word and 5 characters

etc.
.......

play again? y or n: y

Enter Phrase:

Enter another Phrase: Hello Again

Phrase "" has 1 word and 0 characters

Phrase "Hello Again" has 2 words and 11 characters

etc.

Unfortunately I am stuck using the format I am using because my professor wants it that way. Thanks for the responses, I am still trying to find a solution that works based on my constraints.
I figured it out, I just used:

cin.ignore(1);

To get rid of that character that was being assigned to my first variable. It works, so I am happy, but its still mildly annoying that I don't know why that character was being used an input. Oh well, thanks again everyone for your time.
I'm wasn't sure myself when this has happened to me but i just used the getline(cin, string word) and it worked fine...

Anyways, i have also learnt something here because i did not know there was an ignore() function so thank you for posting this qestion on the forums.

Are you able to use ignore on it's own as a function to pause the program for a few seconds?
For some reason I was unable to use getline(cin, string word).

and while the ignore function works, I am running into trouble once the program loops through a few times, again random characters not being ignored. I guess I am using getline() wrong.
Topic archived. No new replies allowed.