numbers in a code

This is a discussion on numbers in a code within the C++ Programming forums, part of the General Programming Boards category; anyone is willing to give me a hint for my small program. My program is basically promote the user to ...

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

    numbers in a code

    anyone is willing to give me a hint for my small program. My program is basically promote the user to enter 12 characters exactly.Then, the program will test this code in four conditions. These conditions are the following:

    1. must be 12 characters //if this condition is completed, we test the second condition.
    2. digits 4 and 5 must be used at least once //if this condition is completed, we test the second condition.
    .
    .
    Once I can solve the second condition I will probably will be able to test all conditions. I am thinking to use the tokenizer to do the job but I am little confused in the way I use it. Anyone has an idea or a hint how to detect that the user has only used 4 or 5 at least once. let's say the user has entered this code: EP2V973D341L

    Code:
    #include <iostream>
    #include <cctype>
    #include<string>
    #include <stdio.h>
    #include <stdlib.h>
    using namespace std;
    
    void newCode();
    void printCode();		
    int main()
    {
    	int tester;
    		while((tester >= 1) || (tester < 3))
    		{
    				cout << "select one of these options\n";
     				cout << "(1) Insert a new code\n";
     				cout << "(2) Print the data\n";
     				cout << "(3) Exit the program\n";
    				cin >> tester;
    		
    	
    		if(tester==1)
    			newCode();		
    		else if (tester==3)
    			cout << "GoodBye";
    			break;
    	 /*else if (tester==3)
    			void exit ( int status );
    
    		else
    			cout << "ERROR!!! You must choose one of the options from 1 to 3";*/
       }
      	return 0;
    	
    }  
    
    void newCode()
    {
    	string serial_number;
    	int length;	
    	
       cout<< "please enter a serial number of 12 characters:";
    	cin >> serial_number;
    	length = serial_number.length();
    
    	if (length!=12 )
    		cout << "The code you have entered cannot be processed because the code must 12 characters exactly";
    	else 
    		cout << "The serial number is:" << serial_number<< endl;
    		
    }

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    First, you should learn to indent:
    http://cpwiki.sf.net/Indentation
    The length should be fine. To validate the input for the 4 and 5 digits, do a loop and check each character in the string for '4' and '5'. Set flags if they're found. When the loop exits, check the flags and see if they're set. Then you can see if the user entered both 4 and 5.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,788
    The std::count function in the <algorithm> header can help in determining this:
    "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
    Registered User
    Join Date
    Jan 2007
    Location
    Euless, TX
    Posts
    144
    I doubt if you'll ever get inside your "while" condition. What is tha value of 'tester' upon entering the while statement? It's either garbage, or if the compiler is nice, it has initialized the value to zero. Still it won't meet the condition will it?

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    It's an or, kcpilot. Every number is either greater than 1 or less than 3, so the while loop never stops.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    No, it will enter it most likely. Sure, it will give an uninitialized used variable error in Visual Studio, but its value is probably >= 1, so it will enter.
    But I do question if the condition is correct. Perhaps it should be more like >= 1 && <= 3?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And using a do - while loop will avoid having to put a dummy value to intialize the a value just to make sure it enters the loop the first time around. A do - while loop is intended for the situation where you want to do something "at least once", whilst the "while" loop is intended for "do this zero or more times".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    58
    First, you should learn to indent:
    http://cpwiki.sf.net/Indentation
    The length should be fine. To validate the input for the 4 and 5 digits, do a loop and check each character in the string for '4' and '5'. Set flags if they're found. When the loop exits, check the flags and see if they're set. Then you can see if the user entered both 4 and 5.
    I do have the idea but I don't know how to write the code because I am very new in programing

    ~~~~

    I have figured this code but is still not working!

    Code:
    	for( int n =0; n==length;n++)
    	{
    		if (serial_number[n] == 4)
    		cout <<serial_number[n];
    	}

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    No, I don't expect that loop to do much - have another look at the tutorial for "for-loops", either in your book that you are learning from, or here on the CProgramming forum.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    Indentation is incorrect. Re-read that article and fix the indenting. Indenting is very important, just as important as writing correct code.
    The code you posted is incorrect.
    The syntax for for loops is:
    for (init; condition; post)
    It will loop as long as the condition is true. Since n != length, it will never loop. It should be n < length (because indexes are 0-based, so length - 1 is actually the last index).
    Flags can be used with bool variables. Just set them to true if you find the appropriate digit.

    Now, go learn indenting, and ask if you don't understand the article.
    Last edited by Elysia; 01-25-2008 at 02:36 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  11. #11
    Registered User
    Join Date
    Jan 2008
    Posts
    58
    my question is that I have the code stored in (serial_number), I do not know how to split each element up and then comparing each element with the value of 4 or 5?

    and about the indentation I have read the article!! thanks for the link!

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    Code:
    bool b1Found = false;
    std::string mystr = "This is a string";
    for (int i = 0; i < mystr.length(); i++)
    {
    	if (mystr[i] == '1') b1Found = true;
    }
    There's an example. You did part of it already.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  2. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 03:17 PM
  3. prime numbers code compile error
    By Tony654321 in forum C Programming
    Replies: 5
    Last Post: 10-10-2004, 10:13 AM
  4. << !! Posting Code? Read this First !! >>
    By biosx in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2002, 11:51 AM
  5. who can sent me code about 8 numbers
    By xacoolboy in forum C Programming
    Replies: 7
    Last Post: 03-08-2002, 06:33 PM

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