Help with a program loop.

Firstly, thank you to everyone who helped me in the other thread; I'm really starting to grasp the concepts of calling functions and variables between functions. :)

My problem this time is that I can't get this 'termination' function to work properly. I want to ask the user if they want to continue the program, and if y/Y, then repeat. If they input anything except for y/Y, then terminate the program.

What I have so far is not terminating the function. I assume my while loop isn't worded properly. Can someone help me tweak it a little?

Here's my program:
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
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
string input();
int countVowels(string);
int reRun();

// ==========================
// Function: main
// ==========================
int main(){
	char run = 'y';

	while (run == 'y' || run == 'Y'){
        string X = input();

	int vCount = countVowels(X);

	cout << "There are " << vCount << " vowels in " 
         << X << " which has " << X.length() 
         << " letters." << endl; 

	char run = reRun();
	}

	return 0;
}

// ==========================
// Function: input
// ==========================
string input(){

	string Y;

	cout << "Please type word: " << endl;
	cin  >> Y;
	cout << endl;

	return Y;
}

// ==========================
// Function: countVowels
// ==========================
int countVowels(string Z){
	int    NewCount = 0;
	int    i = 0;

	while (i < Z.length())
	{
		char Let = Z [i];
		bool isVowel (Let == 'a' || Let == 'e' || Let == 'i' || Let == 'o' || Let == 'u' ||
			          Let == 'A' || Let == 'E' || Let == 'I' || Let == 'O' || Let == 'U');
		if (isVowel == true)
		{
		++NewCount;
		}
		++i;
	}
	return NewCount;
}

// ==========================
// Function: reRun
// ==========================
int reRun(){
   char run2;
   cout << "Do you want to run this program again? (y/n): ";
   cin  >> run2;
   cout << endl; cout << endl;

   return run2;
}
On a side note, I'm also getting the error:
(51): warning C4018: '<' : signed/unsigned mismatch

though the program will works even with this error.
I've only been coding in C++ for about 3 weeks now, so I have no idea what this error means. If someone more experienced could check that out, I'd be grateful!
I tried this again using a boolean return, but the program still runs, even when I return false (which should cause the while loop to finish).

What am I doing wrong?

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 "stdafx.h"
#include <iostream>
#include <string>
using    namespace std;
string   input();
int      countVowels(string);
bool     reRun();

// ==========================
// Function: main
// ==========================
int main(){
	bool run = true;

	while(run){

    string X = input();

	int vCount = countVowels(X);

	cout << "There are " << vCount << " vowels in " 
         << X << " which has " << X.length() 
         << " letters." << endl; 

	bool run = reRun();
	}

	return 0;
}

// ==========================
// Function: input
// ==========================
string input(){

	string Y;

	cout << "Please type word: " << endl;
	cin  >> Y;
	cout << endl;

	return Y;
}

// ==========================
// Function: countVowels
// ==========================
int countVowels(string Z){
	int    NewCount = 0;
	int    i = 0;

	while (i < Z.length())
	{
		char Let = Z [i];
		bool isVowel (Let == 'a' || Let == 'e' || Let == 'i' || Let == 'o' || Let == 'u' ||
			          Let == 'A' || Let == 'E' || Let == 'I' || Let == 'O' || Let == 'U');
		if (isVowel == true)
		{
		++NewCount;
		}
		++i;
	}
	return NewCount;
}

// ==========================
// Function: reRun
// ==========================
bool reRun(){
   char run2;

   cout << "Do you want to run this program again? (y/n): ";
   cin  >> run2;
   cout << endl; cout << endl;
   if (run2 != 'y' || run2 != 'Y')
       return false;
   else
	   return true;
}
Is no one alive right now? >_>
Side note:

My program works just fine -as it should-, but only if I don't use a separate function to call for termination. I really need to know why my termination function isn't working.

Anyway, here's my 100% working code without the separate functions from above (for comparison):

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
#include "stdafx.h"
#include <iostream>
#include <string>
using    namespace std;
string   input();
int      countVowels(string);

// ==========================
// Function: main
// ==========================
int main(){
	
	bool run = true;
	char reRun;

	do{

    string X = input();

	int vCount = countVowels(X);

	cout << "There are " << vCount << " vowels in '" 
         << X << "', which has " << X.length() 
         << " letters." << endl; 

	cout << endl; cout << endl;

	cout << "Do you want to run this program again? (y/n): ";
    cin  >> reRun;

	cout << endl; cout << endl;

	if (reRun == 'y' || reRun == 'Y')
		run = true;
	else
		run = false;

	} while (run);

	return 0;
}

// ==========================
// Function: input
// ==========================
string input(){

	string Y;

	cout << "Please type word: ";
	cin  >> Y;
	cout << endl;

	return Y;
}

// ==========================
// Function: countVowels
// ==========================
int countVowels(string Z){
	int    NewCount = 0;
	int    i = 0;

	while (i < Z.length())
	{
		char Let = Z [i];
		bool isVowel (Let == 'a' || Let == 'e' || Let == 'i' || Let == 'o' || Let == 'u' ||
			          Let == 'A' || Let == 'E' || Let == 'I' || Let == 'O' || Let == 'U');
		if (isVowel == true)
		{
		++NewCount;
		}
		++i;
	}
	return NewCount;
}
In main you have two variables named run. The one you declare in the while loop is not visible in the loop condition, so the while loop can never end.

Line 25 needs to be run = reRun();

The unsigned vs signed is, again, where you compare i to Z.length(). string::length() returns an unsigned value, i is signed.

Edit: One problem wasn't a problem - removed explanation.
Last edited on
In the version of the program in the first post:

1
2
3
4
5
6
7
8
9
10
11
12
13
int main(){
	char run = 'y';

	while (run == 'y' || run == 'Y'){

        // ....

	// char run = reRun(); // *** this defines a new variable
        run = reRun() ;  
	}

	return 0;
}

Okay, I feel silly now.
I was declaring the same variable twice.
Problem solved; thanks.

I still need to do some research on what signed/unsigned bytes are now, but my main problem is solved. Thanks again cire (and thank you too JLBorges).

:)
Last edited on
Topic archived. No new replies allowed.