I need help with void command

This is a discussion on I need help with void command within the C++ Programming forums, part of the General Programming Boards category; I have seen in some peoples code the command void. From what I understand, it would allow you to point ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    48

    I need help with void command

    I have seen in some peoples code the command void. From what I understand, it would allow you to point a person to another part in the program. I have tried this, but could not figure it out. The main idea of what I would try to get, would be a concept of something along the following lines:
    Code:
    #include <iostream>
    using namespace std;
    int main()
                                      //I want to define a place that can be refered to here.
    
    {
        cout<<"Hello.\n";
        cin.get();
        cout<<"Press return to restart the program.\n";
        cin.get();
                                     //I want it to refer to the point above when it reaches this point.
    }
    If anyone can help me with this code, please do. Thanks for the help.
    Razorblade Kiss

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Wait huh? Are you talking about goto statements? I'm usually pretty good at understanding cryptic posts but you stumped me here.

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    48

    Smile Goto....

    Well, that might be what I am looking for. I just thought that I had noticed some things about void in posts where it would goto a spot defined earlier in the program. Goto sounds more appropriate than void anyway, so go ahead and lay it on me if you would please.

  4. #4
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    ???? Well to move around in a program you really should look into functions and logic statements, however the goto statement is what you are asking for here:

    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
    repeat:
        cout<<"Hello.\n";
        cin.get();
        cout<<"Press return to restart the program.\n";
        cin.get();
        goto repeat                            //I want it to refer to the point above when it reaches this point.
    }
    BTW THIS PROGRAM DOES NOT END. DO NOT RUN IT!!!!!!! YOU WOULD NEED TO USE AN IF STATEMENT TO CHECK AND SEE IF THE USER PRESSED ENTER.

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Warning! For teaching you this I may be tarred and feathered by many members of the forums. You too may be punished if spotted for using them. You have been warned

    Ok now that we are done with the legal mumbo jumbo:

    Example:
    Code:
    #include <iostream>
    using namespace std;
    int main(void)  // btw, here is a correct usage of void. void can also mean no return type
                                      //I want to define a place that can be refered to here.
    
    {
      lbl_i_want_to_go_back_to_the_beginning:
        cout<<"Hello.\n";
        cin.get();
        cout<<"Press return to restart the program.\n";
        cin.get();
                                     //I want it to refer to the point above when it reaches this point.
        goto lbl_i_want_to_go_back_to_the_beginning;
    }
    As for void (bolded), it means one or both of the following:

    Example:
    Code:
    void function(int parameter); // function that has no return.
    int function(void); // function that has no parameters.
    
    // and again, you could have:
    void function(void); // function that has no parameters and no return.
    In C++ int main() and int main(void) would have the same meaning, but in C they do not. Also these are not to be confused with void pointers (void *) which are merely a placeholder for any type of pointer.

  6. #6
    Registered User
    Join Date
    Dec 2004
    Posts
    48
    Thank you very much...This is exactly what I was looking for. Now I would ask people to delete it, but from what I hear, nobody else would be able to learn from it! Thanks a lot!

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Its ok, if asked just tell quzah that you were learning it so you know what not to do

    By the way instead of goto you could always do this:

    Example
    Code:
    #include <iostream>
    using namespace std;
    int main(void)  // btw, here is a correct usage of void. void can also mean no return type
                                      //I want to define a place that can be refered to here.
    
    {
      while(1) {
        cout<<"Hello.\n";
        cin.get();
        cout<<"Press return to restart the program.\n";
        cin.get();
                                     //I want it to refer to the point above when it reaches this point.
      }
    }

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Now that you know the wrong way, how about showing you the right way?

    Code:
    #include <iostream>
    using namespace std;
    
    int main(void)
    {
      while(true) {
        cout<<"Hello.\n";
        cin.get();
        cout<<"Press return to restart the program.\n";
        cin.get();
      }
    }
    This is a simple infinite loop. The stuff in the braces will be repeated over and over, until the condition becomes wrong. Since the condition here is a true constant, it will never become false and the loop will loop forever.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Registered User
    Join Date
    Dec 2004
    Posts
    48

    Talking Goto...

    I wasn't trying to make a program that repeats forever, that was just the main concept. What I am actually trying to put it into use is for in a menu style program. After say it asked for a password, if they entered an incorrect password, it would go back and ask for it again and things like that. But I learned more than I bargained for, and found out that many of the people that helped me have nice senses of humor. Thanks everyone

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >For teaching you this I may be tarred and feathered by many members of the forums.
    Only the ignorant ones.

    >In C++ int main() and int main(void) would have the same meaning, but in C they do not.
    In C they have the same meaning as well, provided you don't do something redundant like declare a prototype for main.
    My best code is written with the delete key.

  11. #11
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    True, but lets not use main as an example for the sake of argument its just important that Razorblade Kiss understand that C will treat closed empty paranthesis as a parameter list, whereas C++ will treat it as a function with no arguments.

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >its just important that Razorblade Kiss understand that C will treat closed empty paranthesis as a parameter list
    It depends whether it's a declaration or a definition. But, since everyone is getting into the habit of sending me anonymous bad rep with comments telling me to butt out, I'll butt out. I have better things to do than listen to ungrateful people gripe that I'm making off-topic comments.

    p.s. The month is up, whether it's been 31 days or not.
    My best code is written with the delete key.

  13. #13
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    <- very confused.

    To clear matters up, at most I've only posted negative feedback twice and you know full well as to when and why those were posted (and they were mean and spiteful and I appologized for them). If you are still getting them, I swear its not me.

    Back on topic real quick, abusing closed empty paranthesis isn't exactly good form. But I have to admit it can solve a lot of problems. Especially when it comes to passing C++ objects into C code.

  14. #14
    Not just a squid...
    Join Date
    Sep 2004
    Posts
    25

    On goto

    Looks like I'm a bit late to respond to this post, but I thought I'd add my two cents. I would suggest you take the advice of CornedBee and Master5001 and use loops rather than goto statements, as they are considered obsolete by many people and just plain poor code by others. In my first programming class, for example, my instructor told us if he saw a single goto statement in any program that person would get an "F" for the whole course. I don't know if he was serious but none of us ever used a goto.

  15. #15
    Banned
    Join Date
    Oct 2004
    Posts
    250
    Code:
    #include <iostream>
    using namespace std;
    
    void AVoidFuntion()
    {
    	cout <<"Hello World"<<endl;
    	cin.get();
    }
    int main()
    {
    	AVoidFuntion();
    	return 0;
    }
    EDIT

    I see what your trying to do well it should be something like this.
    Code:
    #include <iostream>
    #include <cstring>
    using namespace std;
    
    void Menu()
    {
    	char password[100];
    
    	cout <<"Please Enter Your PassWord"<<endl;
    	cin.getline(password,100);
    
    	if(strcmp(password, "thepass")==0)
    	{
    		cout <<"The password is correct"<<endl;
    	}
    	else
    	{
    		cout <<"The password is not correct"<<endl;
    	}
    }
    int main()
    {
    	Menu();
    	cin.get();
    	return 0;
    }
    Last edited by cgod; 12-20-2004 at 09:25 PM.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 04:42 PM
  2. ChangeDisplaySettings - Blank?
    By Tonto in forum Windows Programming
    Replies: 13
    Last Post: 12-26-2006, 03:17 PM
  3. Pls repair my basketball program
    By death_messiah12 in forum C++ Programming
    Replies: 10
    Last Post: 12-11-2006, 04:15 AM
  4. i cannot see the mouse arrow
    By peachchentao in forum C Programming
    Replies: 6
    Last Post: 12-10-2006, 03:14 AM
  5. Just one Question?
    By Irish-Slasher in forum C++ Programming
    Replies: 6
    Last Post: 02-12-2002, 09:19 AM

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