Thread: the length of a string code

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    58

    the length of a string code

    hi everyone,
    I am very new in C++ so I don't have much of knowledge about the language. The question is I have promoted my new program to enter 12 characters. So, now I want to check if the user have entered 12 characters exactly not less and not more. The code will include some numbers with some letters EX ( 345D3CC123FA ). How I can do that ? I know that I am going to use If statement but in the condition EX ( if ( my condition) ) I don't know how to tell the check if the user entered 12 characters or not... I hope it is clear..

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    What data type are you reading your string into? Does it have a built-in length function?

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    58
    I don't know excatly what do you mean but this is all what I have gotten so far!

    Code:
    #include<string>
    using namespace std;
    
    	String i;
    	int n;
    	char codeRange[11];
    
    	
    
    int main()
    {
      	cout<< "please enter a serial number of 12 characters:";
    	cin >> i;
    	
    	if (i.length)
    	{
       // code to execute if condition is true
    	}
    	else 
    	{
       // code to execute if condition is false
    	}

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Since you included the <string> header, it looks like you want to use std::string, which does have a length() member function. However, C++ identifier names are case sensitive, so it should be string, not String.

    Oh, and it would be good to avoid global variables.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, what do you want to be true about i.length? That's what needs to go in the if () part.

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Try this:
    Code:
    	if (i.length()==12)
    Todd

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    58
    I have completed my program but it has this Error E2379 CokeRewardCodes.cpp 18: Statement missing ; in function main()
    where is the error in this code ?

    Code:
     
    #include <iostream>
    #include<string>
    using namespace std;
    
    int main()
    {
       string i; 
    	int length=0;	
    	cout<< "please enter a serial number of 12 characters:";
    	cin >> i;
    	length=i.length();
    	
    	if (length!=12 )
    		cout << "The code you have entered cannot be processed because the code must be contained of 12 characters exactly";
    	else (length == 12)
    		cout << " that's correct code";
    
    	return 0;
    }

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    You meant "else if (length == 12)", but you could just write "else" since you already know in that case that length == 12.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    This is starting to become an epidemic.
    Code:
    else (length == 12)
    I didn't get line 18 when I counted, but who knows. Anyway, there's no reason to put a condition on an else statement, because an else statement doesn't need a condition. It's everything else, that is, everything that didn't make the first if true. So remove the condition length==12.

  10. #10
    Registered User
    Join Date
    Jan 2008
    Posts
    58
    yeah that's right .. thanks so much the program is working!

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The problem is with:
    Code:
    else (length == 12)
    It should really just be else.

    Also, I would rename the string i to serial_number, and will not bother with a length variable unless it really is necessary to avoid calling length() multiple times.

    You could also be more consistent with your code indentation.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 03:27 AM
  2. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  3. Replies: 1
    Last Post: 10-31-2005, 11:36 AM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM