String Verification?? Help plz!

This is a discussion on String Verification?? Help plz! within the C++ Programming forums, part of the General Programming Boards category; Hello everyone, I would really appreciate any help anyone can offer on a program I'm working on. The concept is ...

  1. #1
    Self-Confessed Newbie
    Join Date
    Oct 2004
    Posts
    2

    String Verification?? Help plz!

    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:
    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
    If anyone had any insight at all as to why this isn't working, I'd be forever grateful...

  2. #2
    Registered User
    Join Date
    Oct 2004
    Posts
    120
    You declared the vector as a <char>. Char '0' == 48 as an int. so '0' != 0.

    Also...

    Code:
    if (numbers[3] == 0) //This would be the 4th number...
    					bool valid = true; //all these conditions must be true to set the flag
    ...
    index = 4;  //This is the 5th number...

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    Quote Originally Posted by Evangelic04
    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.
    Why use a vector<char> exactly? A string would probably be just as good (or better?).

    That aside, you have a potential problem:

    Code:
    vector<char> numbers(7);
    ...
    while(temp != '\n')
    {
        cin.get(temp);
        cout<<temp<<endl; //verify the string is being read correctly
        numbers[index] = temp; // Dangerous if you don't make sure there is enough room
        index++;
    }
    What if the user keeps on entering more than 7 characters? Bad things happen that's what. At the very least you should be checking to make sure that index is not 7 or greater before attempting that line marked in red above. It would be better to use the push_back function. Also, your code will store the newline character in the vector which is probably not what you intend. A way to perhaps rewrite some of this:

    Code:
    vector<char> numbers; // Don't declare a default size, let it grow as needed
    ...
    cin.get(temp);  // Get a character first before entering the loop to test it for a newline
    while(temp != '\n')
    {
        numbers.push_back(temp); // Add character to end of vector
        cin.get(temp);  // Get next character
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Self-Confessed Newbie
    Join Date
    Oct 2004
    Posts
    2
    Lol, I can't even believe I made such simple mistakes! Thank you both very much for your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21