Cannot get past input

I've searched for the solution online and throughout the forums but couldn't find anything that helped me (although I may have looked over it and simply not understood.) I'm having trouble getting past input in my program, where it takes input and then just stops, as if it's processing, and won't respond to anything other than killing the program. Here is my 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
string getBinaryValueFromUser()
{
    bool test;
    int i;
    string binaryValue;
    bool reset;
    while (reset = true)
	{
		cout << "\nEnter a binary value containing up to 16 digits: ";
		getline(cin, binaryValue);
		do
		{
			for (i = 0; i > binaryValue.length(); i++)
			{
				if (!(binaryValue[i] == 0 || binaryValue[i] == 1)||binaryValue.length() > 16)
				{
					test = true;
					cout << "\nError: Invalid binary value.\nTry again."
						 << "\nPress Enter to continue ...";
					cin.ignore(80,'\n');
				}
				else
					test = false;
			}
		}while (test = true);
	}
    reset = false;
	return binaryValue;
}
You are assigning boolean values to variables within your loop conditions. Try while(reset == true) instead of while(reset = true) in line 7. Also in line 25

It happens to all of us once and awhile.
Last edited on
I've changed a few things around, hoping to have fixed the problem. I've changed all my bool expressions from (bool = true) to (bool == true), and I've initialized test before using it as I was getting a fatal error because of it. Still getting the same problem.

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
string getBinaryValueFromUser()
{
    bool test = true;
    int i;
    string binaryValue;
    bool reset = true;
    while (reset == true)
	{
		cout << "\nEnter a binary value containing up to 16 digits: ";
		getline(cin, binaryValue);
		int c = 0;
		do
		{
			for (i = 0; i > binaryValue.length(); i++)
			{
				if (!(binaryValue[i] == 0 || binaryValue[i] == 1)||binaryValue.length() > 16)
				{
					c++;
					test = true;
					cout << "\nError: Invalid binary value.\nTry again."
						 << "\nPress Enter to continue ...";
					cin.ignore(80,'\n');
				}
				else
					test = false;
			}
		}while (test == true);
	}
    reset = false;
    return binaryValue;
}
if (!(binaryValue[i] == 0 || binaryValue[i] == 1)||binaryValue.length() > 16)
Testing individual characters of a string for digits one and zero should look like this:
(binaryValue[i] == '0')
notice the single quotes around the character.

There's no need to repeatedly test the string length inside the for-loop, just do it once, before the loop begins.

I don't see that you've have you declared binaryValue as an array, because you're accessing an array within line 16.

I know string's can be arrays of characters but I don't think that fact applies to this instance.

Try for line 5
1
2
3
4
5
6
7
int i;
string temp;
string* binaryValue;
cout << "\nEnter a binary value containing up to 16 digits: ";
cin >> temp;
i = temp.length();
binaryValue= new int[i];


don't forget, when you want to discard binaryValue use:
 
delete[] binaryValue;
Last edited on
Found my problem. The < should have been a > in my for-loop. Thanks for the help!
Wait I'm confused, you can use binaryValue as an array without declaring it first as an array?

The std::string class allows the [] operator to be used to access individual characters within the string.
http://www.cplusplus.com/reference/string/string/operator%5B%5D/
:) It ain't easy being cheesy. Or newbie in my case. That is amazing information! Well you learn something new everyday...
Last edited on
This revelation would have made my replace all vowels in a string program soooo much easier. Thanks Chervil, I learn something new from you everyday! If you don't mind me asking what resources(books, tuts?) did you use to learn C++?

My vowels solution :(
1
2
3
4
5
6
7
8
9
	for (int i=0; i < name.length(); i++)
	{
		for (int x=0; x < 6; x++) {
			vowel = vowelArr[x];
			found = name.find(vowel);
		if (found!=string::npos)
			name.replace(found, 1, "z");
		}
	}
Last edited on
what resources(books, tuts?) did you use to learn C++?

That's difficult to answer, as I learned other languages first, and could apply some previous knowledge during the learning process.

However, I do recommend getting a good book (or several!) and work through them carefully. I find the reference section on this site has been very helpful (though it is still useful to look elsewhere too).
Topic archived. No new replies allowed.