Hello everyone, I would really appreciate any help anyone can offer on a program I'm working on. The concept is simple...The user should enter a string of numbers, and the program is going to verify if the string is "valid" or not. A valid string is defined as a sring that starts with 101, and ends with 11, with one or more 0s in between. Confusing, I know.
Basicially, "101011" and "101000011" are valid strings, but stuff like "001101" or "1010010" are not valid.
So I figure I will use a loop to grab each individual character of a string that the user enters, and store each character inside an element of a vector, which I called "numbers". Of course I use a vector because valid strings can be any size, as long as they fit the pattern outlined above. Then I can just go back, and verify each individual element in the vector, right? If any element doesn't match the pattern, then I tell the user that the string is invalid.
Maybe that wasn't so clear, but I outlined it on paper and it makes since in my head. So I spent an hour or five actually coding the thing, got it to compile, and ran it...but even if I enter a string that should be valid, such as "1010011", the program tells me that it is invalid, plus I get a little window telling me I have a debug error.
Here is my code, with comments:
If anyone had any insight at all as to why this isn't working, I'd be forever grateful...Code:#include <iostream> #include <vector> #include <iomanip> using namespace std; bool comparison(); //function prototype vector<char> numbers(7); //the vector has global scope so that it can be used in //multiple functions int index = 0; int main() { char temp; //the user enters the whole number, and these lines get the //first digit and store it in the first element of a vector "numbers" cin.get(temp); cout<<temp<<endl; numbers[index] = temp; index++; //this part of the function simply gets the numbers that the user //entered, and puts each one into a seperate element of the //vector called numbers. while(temp != '\n') { cin.get(temp); cout<<temp<<endl; //verify the string is being read correctly numbers[index] = temp; index++; } //call a second function for the very long comparison process if(comparison()) cout<<"You have entered a valid string."<<endl; else cout<<"You have not entered a valid string."<<endl; return 0; } bool comparison() { bool valid = false; //validate first three digits with a series of if statements if (numbers[0] == 1) if (numbers[1] == 0) if (numbers[2] == 1) if (numbers[3] == 0) bool valid = true; //all these conditions must be true to set the flag //if the first three digits are 1,0,1 then continue if (valid == true) { index = 4; //set index to three to access the third element of the vector //this is a for loop that continues for however many 0's for(int hold = 0; hold == 0; index++) { hold = numbers[index]; //verify that the array isn't over 100 elements large if(index == 100) { cout<<"The string you have entered is too large."<<endl; exit(0); } }//end of for statement }//end of if statement //reset the flag for convience valid = false; //now verify that the final two digits are 1 and 1 if (numbers[index] == 1) { index++; if(numbers[index] == 1) return true; //if the program has made it this far, end the function //with a returned true } else return false; } //end of function