Thread: printing & sending error message

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

    printing & sending error message

    This program was really pain for me to do it but anyways my program must do the following steps:

    1. promote the user for 12 characters.
    2. check conditions of the 12 characters are the following:
    a. The digits 4 and 5 must appear at least once.
    b. Either the digit 8 and 9 must appear at least once.
    c. The only valid letters are A,L,P,R,T,V,W, and X.
    d. Each four-character subsequence (1-4, 5-8, 9-12) must contain at least one digit and at least one letter.
    3. sends am error message if the code doesn't follow all the condition.
    4.store the code into an array
    5. print the code from the menu


    my problem:
    1. In my code my program doesn't send an error message when I enter the wrong code that doesn't follow the conditions.

    2. In my code my program doesn't print the code that follows the conditions from the menu interface.


    anyone knows what's the wrong in the code ?



    Code:
    #include <iostream>
    #include <cctype>
    #include<string>
    #include <stdio.h>
    #include <stdlib.h>
    using namespace std;
    
    void newCode();
    string billy [12];
    
    int main()
    {
    	string billy [12];
    	int tester;
    		while((tester >= 1) || (tester < 4))
    		{
    			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==2)
    				for (int i=0; i<13; i++) 
    				{
         		  		 cout << billy[i] << endl;
        			        }
    		 	else if (tester==3)
    				break;
    			else
    			cout << "ERROR!!! You must choose one of the options from 1 to 3";
                  }
    return 0;
    
    }  
    
    void newCode()
    {
    	string serial_number;
    	int length;	
       
    	size_t found,found2,found3,found4;
    	
       cout<< "please enter a serial number of 12 characters:";
    	cin >> serial_number;
    	length = serial_number.length();
    	
    	std::string mystr = serial_number;
    	string str1 ("4");
    	string str2 ("5");
    	string str3 ("8");
    	string str4 ("9");
    /*	string str5 ("A");
    	string str6 ("L");
    	string str7 ("P");
    	string str8 ("R");
    	string str9 ("T");
    	string str10 ("V");
    	string str11("W");
    	string str12("X");
    	
    	found= serial_number.find(str1);
    	found2= serial_number.find(str2);
    	found3= serial_number.find(str3);
    	found4= serial_number.find(str4);
    /*	found5= serial_number.find(str5);
    	found6= serial_number.find(str6);
    	found7= serial_number.find(str7);
    	found8= serial_number.find(str8);
    	found9= serial_number.find(str9);
    	found10= serial_number.find(str10);
    	found11= serial_number.find(str11);
    	found12= serial_number.find(str12)*/
    		
    	if (length!=12 )
    		cout << "The code you have entered cannot be processed because the code must 12 characters exactly";
    	else if ((found!=string::npos) && (found2!=string::npos))
       		if ((found3!=string::npos) || (found4!=string::npos))
    					for(int i = 0; i< 4; i++)
    					{
    						if ((mystr[i] == 'A') || (mystr[i] == 'L') || (mystr[i] == 'P') || (mystr[i] == 'R') || (mystr[i] == 'T') || (mystr[i] == 'V')||(mystr[i] == 'W')||(mystr[i] == 'X'))
    							for(int i = 4; i< 7; i++)
    							{
    								if ((mystr[i] == 'A') || (mystr[i] == 'L') || (mystr[i] == 'P') || (mystr[i] == 'R') || (mystr[i] == 'T') || (mystr[i] == 'V')||(mystr[i] == 'W')||(mystr[i] == 'X'))
    									for(int i = 7; i< 11; i++)
    									{
    										if ((mystr[i] == 'A') || (mystr[i] == 'L') || (mystr[i] == 'P') || (mystr[i] == 'R') || (mystr[i] == 'T') || (mystr[i] == 'V')||(mystr[i] == 'W')||(mystr[i] == 'X'))
    											for(int i=0; i<length;i++)
    											{
    												billy[i]=mystr[i];
    											}
    										else
    											cout << "You are mising one or more of the following:\n";
    											cout << "   1. The digits 4 and 5 must appear at least once.\n";
    											cout << "   2. Either the digit 8 and 9 must appear at least once.\n";
    											cout << "   3. The only valid letters are A,L,P,R,T,V,W, and X.\n";
    											cout << "   3. Each four-character subsquence (1-4, 5-8, 9-12) must contaon at least one digit and at least one letter.\n";
    
    									}
    							}
    					}
    														
    		
    }
    Last edited by aama100; 01-26-2008 at 06:24 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by aama100 View Post
    This program was really pain for me to do it but anyways my program must do the following steps:

    1. promote the user for 12 characters.
    2. check conditions of the 12 characters are the following:
    a. The digits 4 and 5 must appear at least once.
    b. Either the digit 8 and 9 must appear at least once.
    c. The only valid letters are A,L,P,R,T,V,W, and X.
    d. Each four-character subsequence (1-4, 5-8, 9-12) must contain at least one digit and at least one letter.
    3. sends am error message if the code doesn't follow all the condition.
    4.store the code into an array
    5. print the code from the menu


    my problem:
    1. In my code my program doesn't send an error message when I enter the wrong code that doesn't follow the conditions.

    2. In my code my program doesn't print the code that follows the conditions from the menu interface.


    anyone knows what's the wrong in the code ?
    1. You're not checking the conditions properly. You've commented out all the found checks, you're checking this mystr thing which I can't find, even though you read the code into serial_number, and even if those were fixed, your else statement only applies to the last if, so if you fail the first check nothing gets printed.

    2. Your billy[] in main hides the global billy[], which is the only one that newCode can see, so that's where the serial number goes.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    58
    I have stored the serial_number into string mystr
    Code:
    	std::string mystr = serial_number;
    then I checked ever single step from:
    Code:
    if (length!=12 )
    		cout << "The code you have entered cannot be processed because the code must 12 characters exactly";
    	else if ((found!=string::npos) && (found2!=string::npos))
       		if ((found3!=string::npos) || (found4!=string::npos))
    					for(int i = 0; i< 4; i++)
    					{
    						if ((mystr[i] == 'A') || (mystr[i] == 'L') || (mystr[i] == 'P') || (mystr[i] == 'R') || (mystr[i] == 'T') || (mystr[i] == 'V')||(mystr[i] == 'W')||(mystr[i] == 'X'))
    							for(int i = 4; i< 7; i++)
    							{
    								if ((mystr[i] == 'A') || (mystr[i] == 'L') || (mystr[i] == 'P') || (mystr[i] == 'R') || (mystr[i] == 'T') || (mystr[i] == 'V')||(mystr[i] == 'W')||(mystr[i] == 'X'))
    									for(int i = 7; i< 11; i++)
    									{
    										if ((mystr[i] == 'A') || (mystr[i] == 'L') || (mystr[i] == 'P') || (mystr[i] == 'R') || (mystr[i] == 'T') || (mystr[i] == 'V')||(mystr[i] == 'W')||(mystr[i] == 'X'))
    											for(int i=0; i<length;i++)
    											{
    												billy[i]=mystr[i];
    											}
    and all the conditions are working except:

    Code:
    									
    											for(int i=0; i<length;i++)
    											{
    												billy[i]=mystr[i];
    											}

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Okay, I missed that variable declaration. But: this billy is not the same billy as in main.

    Edit to add: You can use different letters for each of your for-loops too. Oh, and consider the serial code AAA4 AAA5 AAA8: you'll copy the string into billy 27 times, and print an error message nine times.

    Edit to add again: AND, you don't have all those error statements in a block, so the first one "You are ..." only appears nine times, but the rest appear each of the thirty-six times. Exciting! If I were you, I would go back to the flowchart and look a bit more carefully.
    Last edited by tabstop; 01-26-2008 at 06:53 PM.

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    58
    I think all the conditions except the last one are working properly because I tested each one before I do the one after but there is something hidden that I don't really know it is not working the way I want

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Rather than that huge mess of code, consider this approach.

    You have 4 conditions, so use 4 functions.
    Eg.
    bool checkCond2a( std::string input );

    Then checkValid is simply
    Code:
    bool checkValid ( std::string input ) {
        return checkCond2a(input) && 
               checkCond2b(input) && 
               checkCond2c(input) && 
               checkCond2d(input);
    }
    With a bit of debug, it would become very obvious which test doesn't work and why.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C# Printing Problem
    By silverlight001 in forum C# Programming
    Replies: 0
    Last Post: 03-23-2009, 01:13 AM
  2. Printing Lines to .txt File
    By Programmer3922 in forum C Programming
    Replies: 2
    Last Post: 08-02-2008, 12:45 PM
  3. generic printing preferences dialog box
    By stanlvw in forum Windows Programming
    Replies: 8
    Last Post: 06-27-2008, 02:20 AM
  4. printing data to a file
    By coralreef in forum C Programming
    Replies: 3
    Last Post: 11-02-2006, 08:10 PM
  5. need help relating printing?
    By omarlodhi in forum Linux Programming
    Replies: 0
    Last Post: 03-03-2006, 04:46 AM