Thread: Need Help Again

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    30

    Question Need Help Again

    ok, here is the stupid code:

    Code:
    #include <stdio.h>
    #include <windows.h>
    #include <iostream>
    using namespace std;
    
    void sentencecall();
    void clrscr(void);
    
    int main(){
        int j, A;
        char x[256];
    	char str[51];
        sentencecall();
    	if(strlen(x)>50){
    		cout << "The Sentence was too long\n\nRESTARTING...." << endl;
    		clrscr();
    		main();
    	}
        else {
            for(j = 0; j < strlen(x);j++){
    			str[j] = x[j];
    		}
    	}
    	for(A = 0; A < str[j]; A++){
            if(str[A]==32){str[A]=0;}
            if(str[A]==97){str[A]=01;}
            if(str[A]==98){str[A]=02;}
            if(str[A]==99){str[A]=03;}
            if(str[A]==100){str[A]=04;}
            if(str[A]==101){str[A]=05;}
            if(str[A]==102){str[A]=06;}
            if(str[A]==103){str[A]=07;}
            if(str[A]==104){str[A]=27;}
            if(str[A]==105){str[A]=28;}
            if(str[A]==106){str[A]=10;}
            if(str[A]==107){str[A]=11;}
            if(str[A]==108){str[A]=12;}
            if(str[A]==109){str[A]=13;}
            if(str[A]==110){str[A]=14;}
            if(str[A]==111){str[A]=15;}
            if(str[A]==112){str[A]=16;}
            if(str[A]==113){str[A]=17;}
            if(str[A]==114){str[A]=18;}
            if(str[A]==115){str[A]=19;}
            if(str[A]==116){str[A]=20;}
            if(str[A]==117){str[A]=21;}
            if(str[A]==118){str[A]=22;}
            if(str[A]==119){str[A]=23;}
            if(str[A]==120){str[A]=24;}
            if(str[A]==121){str[A]=25;}
            if(str[A]==122){str[A]=26;}
            cout<< str[A] <<flush;
        }
        cout<<"\n\n";
    	system("pause");
    }
    
    void sentencecall(){
        cout << "Enter the sentence you wish to have encrypted." << endl;
    	char x[256];
    	char str[51];
    	cin >> x;
    	cout << "\n\n";
    	return;
    }
    
    void clrscr(void)
    {
        COORD                       coordScreen = { 0, 0 };
        DWORD                       cCharsWritten;
        CONSOLE_SCREEN_BUFFER_INFO  csbi;
        DWORD                       dwConSize;
        HANDLE                      hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
        GetConsoleScreenBufferInfo(hConsole, &csbi);
        dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
        FillConsoleOutputCharacter(hConsole, TEXT(' '), 
                                   dwConSize, coordScreen, &cCharsWritten);
        GetConsoleScreenBufferInfo(hConsole, &csbi);
        FillConsoleOutputAttribute(hConsole, csbi.wAttributes, 
                                   dwConSize, coordScreen, &cCharsWritten);
        SetConsoleCursorPosition(hConsole, coordScreen);
    }
    it will not display the message if more than 50 characters are inputed, it will not display this: "cout<< str[A] <<flush;" whne i want it to....i think everything is rgiht, but evidently not....anyone up to the challenge?
    ADRUMSOLO4U

  2. #2
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    ah.. i've found somethign interesting..

    strlen(variable);

    the return of it can not be compared with a number, but
    you can assign w/e it returns to an int and then use the int variable instead of the "strlen(x);" and then it will work!

  3. #3
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    Code:
    #include <stdio.h>
    #include <windows.h>
    #include <iostream>
    using namespace std;
    int ab;
    void sentencecall();
    void clrscr(void);
    
    int main(){
        int j, A;
        char x[256];
    	char str[51];
        sentencecall();
    	
    	if(ab > 50){
    		cout << "The Sentence was too long\n\nRESTARTING...." << endl;
    		clrscr();
    		main();
    	}
        else {
            for(j = 0; j < ab;j++){
    			str[j] = x[j];
    		}
    	}
    	for(A = 0; A < str[j]; A++){
            if(str[A]==32){str[A]=0;}
            if(str[A]==97){str[A]=01;}
            if(str[A]==98){str[A]=02;}
            if(str[A]==99){str[A]=03;}
            if(str[A]==100){str[A]=04;}
            if(str[A]==101){str[A]=05;}
            if(str[A]==102){str[A]=06;}
            if(str[A]==103){str[A]=07;}
            if(str[A]==104){str[A]=27;}
            if(str[A]==105){str[A]=28;}
            if(str[A]==106){str[A]=10;}
            if(str[A]==107){str[A]=11;}
            if(str[A]==108){str[A]=12;}
            if(str[A]==109){str[A]=13;}
            if(str[A]==110){str[A]=14;}
            if(str[A]==111){str[A]=15;}
            if(str[A]==112){str[A]=16;}
            if(str[A]==113){str[A]=17;}
            if(str[A]==114){str[A]=18;}
            if(str[A]==115){str[A]=19;}
            if(str[A]==116){str[A]=20;}
            if(str[A]==117){str[A]=21;}
            if(str[A]==118){str[A]=22;}
            if(str[A]==119){str[A]=23;}
            if(str[A]==120){str[A]=24;}
            if(str[A]==121){str[A]=25;}
            if(str[A]==122){str[A]=26;}
            cout<< str[A] <<flush;
        }
        cout<<"\n\n";
    	system("pause");
    }
    
    void sentencecall(){
        cout << "Enter the sentence you wish to have encrypted." << endl;
    	char x[256];
    	
    	char str[51];
    	cin >> x;
    	cout << "\n\n";
    	ab = strlen(x);
    	return;
    }
    
    void clrscr(void)
    {
        COORD                       coordScreen = { 0, 0 };
        DWORD                       cCharsWritten;
        CONSOLE_SCREEN_BUFFER_INFO  csbi;
        DWORD                       dwConSize;
        HANDLE                      hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
        GetConsoleScreenBufferInfo(hConsole, &csbi);
        dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
        FillConsoleOutputCharacter(hConsole, TEXT(' '), 
                                   dwConSize, coordScreen, &cCharsWritten);
        GetConsoleScreenBufferInfo(hConsole, &csbi);
        FillConsoleOutputAttribute(hConsole, csbi.wAttributes, 
                                   dwConSize, coordScreen, &cCharsWritten);
        SetConsoleCursorPosition(hConsole, coordScreen);
    }
    A working code

    what i did was add a global variable that will hold hte value of x.

    so the if its above 50 it does whats after th if statement if its less than 50 it does whats in the else statement..

    BTW you cannot rerun main()!

    what i suggest is do everything in void functions..

  4. #4
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Eh... strlen can be compared to a number (integer), as it returns an integer. At any rate, the problem is that the xs are not the same (function scope, and you never passed it as a parameter). Try passing x to your function, modify the contents of the character array, and then run your code (without the globals). And yes, recursive use of main is bad -- don't do it (conider using a loop instead).
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed