code wont work

This is a discussion on code wont work within the C++ Programming forums, part of the General Programming Boards category; somethings wrong with my code its my first time using void Code: #include <cstdlib> #include <iostream> #include <fstream> #include <string> ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    196

    code wont work

    somethings wrong with my code its my first time using void
    Code:
    #include <cstdlib>
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
    int choicemenu;
    int yes_or_no;
    void select;
    
    struct charops {
    string name;
    int lvl;
    double exp;
    int att;
    int def;
    int mage;
    int money;
    };
    struct gameops{
    void play();
    void quit();
    void tut();
    int nam();
    }; 
    cout<<"welcome to destiny \n";
    cout<<"where your destiny becomes a vitual reality \n\n\n";
    cout<<"please choose an option\n\n\n\n";
    cout<<"1. play game"<< endl;
    cout<<"2. tutorial"<< endl;
    cout<<"3. credits"<<endl;
    cout<<"4. quit\n\n\n\n\n\n\n"<<endl;
    cin.get();
    switch( choicemenu )
    case 1:
    cout<<"you have chosen play game\n";
    cout<<"wake up...\n";
    cin.get();
    cout<<"wake up....\n";
    cin.get();
    cout<<"WAKE UP!!!!!!\n";
    cin.get();
    cout<<"finally you're awake\n";
    cin.get();
    cout<<"good morning johnathon\n";
    cout<<"that is your name right?\n";
    cin.get();
    cout<<"y/n\n";
    cin>>yes_or_no;
    if (select == "yes" || select == "Yes" )
    {
       play();
    }
    if (select == "no" || select == "No" )
    {
       nam();
    }        
    }
    of coarse its not done but can you tell me why its not working so far?
    heres the compile log im using dev-c++
    Compiler: Default compiler
    Executing g++.exe...
    g++.exe "C:\Dev-Cpp\textrpg.cpp" -o "C:\Dev-Cpp\textrpg.exe" -g3 -I"C:\Dev-Cpp\lib\gcc\mingw32\3.4.2\include" -I"C:\Dev-Cpp\include\c++\3.4.2\backward" -I"C:\Dev-Cpp\include\c++\3.4.2\mingw32" -I"C:\Dev-Cpp\include\c++\3.4.2" -I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib" -g3
    C:\Dev-Cpp\textrpg.cpp: In function `int main()':
    C:\Dev-Cpp\textrpg.cpp:12: error: variable or field `select' declared void

    C:\Dev-Cpp\textrpg.cpp:53: error: ISO C++ forbids comparison between pointer and integer
    C:\Dev-Cpp\textrpg.cpp:53: error: ISO C++ forbids comparison between pointer and integer
    C:\Dev-Cpp\textrpg.cpp:55: error: `play' undeclared (first use this function)
    C:\Dev-Cpp\textrpg.cpp:55: error: (Each undeclared identifier is reported only once for each function it appears in.)
    C:\Dev-Cpp\textrpg.cpp:57: error: ISO C++ forbids comparison between pointer and integer

    C:\Dev-Cpp\textrpg.cpp:57: error: ISO C++ forbids comparison between pointer and integer
    C:\Dev-Cpp\textrpg.cpp:59: error: `nam' undeclared (first use this function)

    Execution terminated

  2. #2
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    You cannot declare a variable's type as void... that makes no sense. void means nothing. A function can return nothing, but a variable _must_ have a type. The closest thing would be a void *, which is probably not what you want. What is it that select should do?

    Also, you cin >> yes_or_no, but you compare select to a string? Try using cin >> some_variable where some_variable is of type std::string.

    EDIT: play() and nam() are functions of struct gameops. Delcare a variable of this type (i.e., gameops my_game_ops; ) and call the function like you would for a class.

    EDIT: Clarifications & examples:
    A function can return void:
    Code:
    void my_func(int a, int b)
    {
      cout << "A and b! " << a << ' ' << b << endl;
    }
    my_func() returns nothing, no integer, zip. Hence, it can't be used with most (all?) operators:
    Code:
    int x = my_func(2, 3); // Illegal!!
    As opposed to a function that _can_ return something:
    Code:
    int cool_func(int a, int b)
    {
      return a + b;
    }
    Then
    Code:
    int x = cool_func(2, 3); // Legal!!
    Variables can't be declared void. (What would they hold, and how much space would it take up?)
    Last edited by Cactus_Hugger; 07-02-2006 at 10:06 PM.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    196
    can you please edit my code and show me what you mean im not sure what to do

    also im having trouble understnading classes at this time so i dont know what you mean by that

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    Your writing code that is way over your head, right now. Reread your book or whatever you're learning from and make sure you understand it. There really isn't much we can tell you because some of the errors are so basic that explaining it would require us to explain entire sections of C. There are books and tutorials online that explain structs and strings including on this site.
    Sent from my iPadŽ

  5. #5
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    I'm not quite sure what you intend to do to parts of your code. I edited my above post, included some examples, hopefully that'll help a tad.
    Basically, to start:
    Change void select to string select, then replace cin >> yes_or_no; with cin >> select;.

    Also, what does switch(choicemenu) do? You never store a value to choicemenu, so the result is undefined. Initialize your variables for starters. (And if you compile with -Wall, gcc will catch this.)

    *agrees w/ SlyMaelstrom*
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  6. #6
    Registered User
    Join Date
    Sep 2005
    Posts
    196
    haha yah i guess the code is over my head well i copied this entire speech into a notepad file for future reference thanks for the help

  7. #7
    Registered User
    Join Date
    Sep 2005
    Posts
    196
    oh for those who actually want to know i really like this website and i use it often


    http://www.zeuscmd.com/tutorials/cpl...asicOutput.php

  8. #8
    Registered User
    Join Date
    May 2006
    Posts
    4
    Also I think you want to replace the first cin.get() with:

    choicemenu = cin.get();

    Although remember if someone doesn't enter a int (they type in letters) you will still get an int put into choicemenu.

    p.s. indent your code
    Last edited by Dave3of5; 07-03-2006 at 05:44 AM.

  9. #9
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    Quote Originally Posted by Dave3of5
    Also I think you want to replace the first cin.get() with:

    choicemenu = cin.get();
    You mean
    Code:
    cin.get(choicemenu);
    The get method returns a reference to the istream.
    Sent from my iPadŽ

  10. #10
    Registered User
    Join Date
    May 2006
    Posts
    4
    Quote Originally Posted by SlyMaelstrom
    You mean
    Code:
    cin.get(choicemenu);
    The get method returns a reference to the istream.
    Yes sorry

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-15-2007, 12:36 AM
  2. Very short code tt never work...please help
    By newbie1234 in forum C Programming
    Replies: 7
    Last Post: 05-23-2006, 11:46 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. cant get code to work
    By duffy in forum C Programming
    Replies: 13
    Last Post: 10-20-2002, 05:23 AM
  5. << !! Posting Code? Read this First !! >>
    By kermi3 in forum Linux Programming
    Replies: 0
    Last Post: 10-14-2002, 01:30 PM

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