Thread: Need Help

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

    Need Help

    i am trying to put the length width part into it's own function, so that i dont have a code repeat...i cant seem to be able to do this....could you make some suggestions or, even better, edit the code? thanks

    Code:
    #include <iostream>	
    using namespace std;
    void mainA();
    void mainB();
    void end();
    int main()
    {
        cout<<"Enter 1 for the object to have area,\nEnter 2 for the object not to have area: ";
        int A;
        cin>> A;
        cout<<"\n";
        if (A != 1 & A != 2){
            main();
        }
        else{
            if (A != 2){
                mainA();
            }
            else if (A != 1){
                mainB();
            }
        }
    }
    void mainA(){
        cout<<"Input The Length: ";
        int A;
        cin>> A;
        cout<<"Input The Width: ";
        int B;
        cin>> B;
        cout<<"\n\n";
        for (int a = 0; a < A; a++){
            for (int b = 0; b < B; b++){
                cout<<"O";
            }
            cout<<"\n";
        }        
        end();
    }
    void mainB()
    {
        cout<<"Input The Length: ";
        int A;
        cin>> A;
        cout<<"Input The Width: ";
        int B;
        cin>> B;
        cout<<"\n\n";
        for (int a = 0; a < B; a++){
            cout<<"O";
        }
        if (A > 1){
            cout<<"\n";
            if (B > 1){
                for (int a = 0; a < (A - 2); a++){
                    cout<<"O";
                    for (int b = 0; b < (B - 2); b++){
                        cout<<" ";
                    }
                    cout<<"O\n";
                }
                for (int c = 0; c < B; c++){
                    cout<<"O";
                }
            }
            else{
                for (int a = 0; a < (A - 1); a++){
                    cout<<"O\n";
                }
            }
        }
        else{
            cout<<"\n";
        }
        end();
    }
    void end()
    {
        cout<<"\n\n\n";
        cout<<"This Program was created by: Joshua\n";
        cout<<"On 1-29-05\n\n";
        int Z;
        cin>> Z;
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    30
    i was also wondering if you could help me with replacing the O with some kind of line. If you compile the code and run the program, you see that the boxes look like this

    OOOOOOO
    OOOOOOO
    OOOOOOO

    i only know how to use cout<< to display text.....
    i would like to learn how to do the actual visual side of C++, and i would appreciate it, if anyone could give me a tutorial website that i could learn how to do it.
    ADRUMSOLO4U

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    of course the "boxes" will look like O's when you have this in your code
    Code:
    cout<<"O";
    this site has many tutorials
    When no one helps you out. Call google();

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    30
    but i have looked, and maybe i missed it, but when i put this (look bleow) into a funtion, and then call the function in mainA or mainB it says that there was no such int, and when i create that, it is not the same value as in the new function(below)

    void mainAA(){
    cout<<"Input The Length: ";
    int A;
    cin>> A;
    cout<<"Input The Width: ";
    int B;
    cin>> B;
    cout<<"\n\n";
    }
    ADRUMSOLO4U

  5. #5
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    Code:
    void mainAA(){
         
         int A, B;
         
         cout<<"Input The Length: ";
         cin>> A;
         cout<<"Input The Width: ";
         cin>> B;
         cout<<"\n\n";
    
    }
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  6. #6
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    You could declare int A and B globally rather then within the void mainAA function and that should fix your problem

  7. #7
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    Here is a good link on console programming . You will definetly gain some knowledge going through the tutorials.

    There are more than just console tuts here so scroll down slighlty and you will see the 6 part console tutorial.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why on earth did you call your functions "mainA" and "mainB"? They've got to be some of the most unintuitive function names I've ever seen.

    Also, did you mean to have:
    Code:
    if (A != 1 & A != 2){


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    30
    Here's what i did:

    Code:
    #include <iostream>	
    using namespace std;
    void mainA();
    void mainB();
    void end();
    int main()
    {
        int A, B, C;
        cout<<"Enter 1 for the object to have area OR Enter 2 for the object not to have area: ";
        cin>> A;
        cout<<"Input The Length: ";
        cin>> B;
        cout<<"Input The Width: ";
        cin>> C;
        cout<<"\n\n";
        if (A != 1 & A != 2){
            main();
        }
        else{
            if (A != 2){
                mainA();
            }
            else if (A != 1){
                mainB();
            }
        }
    }
    void mainA(){
        int B, C;
        for (int a = 0; a < B; a++){
            for (int b = 0; b < C; b++){
                cout<<"O";
            }
            cout<<"\n";
        }        
        end();
    }
    void mainB()
    {
        int B, C;
        for (int a = 0; a < C; a++){
            cout<<"O";
        }
        if (B > 1){
            cout<<"\n";
            if (C > 1){
                for (int a = 0; a < (B - 2); a++){
                    cout<<"O";
                    for (int b = 0; b < (C - 2); b++){
                        cout<<" ";
                    }
                    cout<<"O\n";
                }
                for (int c = 0; c < C; c++){
                    cout<<"O";
                }
            }
            else{
                for (int a = 0; a < (B - 1); a++){
                    cout<<"O\n";
                }
            }
        }
        else{
            cout<<"\n";
        }
        end();
    }
    void end()
    {
        cout<<"\n\n\n";
        cout<<"This Program was created by: Joshua\n";
        cout<<"On 1-29-05\n\n";
        int Z;
        cin>> Z;
    }
    The compiler allowed it to be compiled, but when i ran the program when you enter all the values, it will keep displaying O
    It continues to scroll, and doesnt stop.

    Q: What Did I do Wrong?
    because i cant see what is wrong about it.
    ADRUMSOLO4U

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    30
    TO quzah:

    yes, i did, that repeats the program if you do not enter 1 or 2
    ADRUMSOLO4U

  11. #11
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    You variables in main(), aka A, B, C are unique to main(). You will not be able to use them outside of this function. To access them you would need to make the variables global or pass them to the function.
    Code:
    #include <iostream>
    
    using namespace std;
    
    int A; //global
    
    void myFunc(int); //function takes an integer value
    
    int main(void){
    
               int B; //local
    
               cout<< "Enter A: ";
               cin >> A;
               cin.get();
               cout << "Enter B: ";
               cin >> B;
    
               myFunc(B);
     
               cin.get();
               return 0;
    }
    void myFunc(int C) { //can use whatever variable name you want
    
               cout << A;
               cout << C;
    }
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  12. #12
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    I'd recommend adding 2 functions. getLength() and getWidth() each of which returns an integer. That way you don't need to use any globals.
    Code:
    int getLength()
    {
        int A;
        
        cout << "Input The Length: ";
        cin >> A;
        cout << endl;
        
        return A;
    }
    Then in the calling functions you can use int A = getLength();. And do the same for getWidth().

    [edit] Now that I think about it, 2 functions would be redundant since you can use 1 and name it something like getSide.

    Example...
    Code:
    void mainA()
    {
        int A = getLength();
        int B = getWidth();
    
        for (int a = 0; a < A; a++){
            for (int b = 0; b < B; b++){
                cout<<"O";
            }
            cout<<"\n";
        }        
    }
    Additionally, clean up your int main(). I shudder whenever I see a recursion to main(). Here's an example that I cleaned up for you...
    Code:
    int main()
    {
        int A = 0;
        
        do {
            
            cout<<"Enter 1 for the object to have area,\nEnter 2 for the object not to have area: ";
            cin>> A;
            cout<<"\n";
            
            if (A == 1)
                mainA();
            else if (A == 2)
                mainB();
            
        } while ( A != 1 && A != 2 );
        
        end(); // remove this function call from mainA and mainB
        
        return 0;
    }
    Last edited by Scribbler; 01-31-2005 at 09:04 PM.

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by adrumsolo4u
    TO quzah:

    yes, i did, that repeats the program if you do not enter 1 or 2
    No. You didn't. That' is a bitwise AND. You wanted the logical AND, which is two ampersands, not one:
    Code:
    if( foo != bar && foo != baz )
    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    30
    thank you all for your help! now i know what to do in a situation where i need to do something like this.
    ADRUMSOLO4U

Popular pages Recent additions subscribe to a feed