Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New to the CF scene
    Join Date
    Jul 2007
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Having a problem in C++

    I'm having a problem with calling for a value from with a subroutine. I really can't figure out what's wrong with the code as it currently stands. Any help with this would be greatly appreciated.

    ___________________________________________________________

    #include <cstdlib>
    #include <iostream>

    using namespace std;

    int main(int argc, char *argv[])
    {
    int num1, num2;
    cout << "How many?";
    cin >> num1;
    cout << "Sides?";
    cin >> num2;
    start:
    {
    num1 = num1-1;
    int roll();
    {
    return(rand() % num2 + 1);
    }
    int roll1;
    roll1 = roll();
    int total;
    total = total + roll1;
    if
    (num1>0); goto start;
    goto endgame;
    }
    endgame:
    {
    cout << "Total equals" << total;}
    system("PAUSE");
    return EXIT_SUCCESS;
    }

  • #2
    New Coder
    Join Date
    Jul 2007
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I don't know what you are trying to do. If you are declaring a function rool, you're making two misstakes.

    1. Functions aren't declared inside of other functions in C/C++, as they can be in PHP for example. You always declare functions in the bottom scope. among the functions in main(). And if main() is going to find function roll(), roll() will also have to be declared before main(), in other words over it, else you will have to specify that you are going to declare roll() laer, by typing roll(); , also that before main().

    2. Second, you've added a semi colon after the parentesis, when declaring a function you dont use semicolor other than inside the function.

    So it should look something like this:

    Code:
    #include <cstdlib>
    #include <iostream>
    
    roll() {
      return(rand() % num2 + 1);
    }
    
    int main(int argc, char *argv[]) {
      ...
      int roll1;
      roll1 = roll();
      ...
    }
    Or maybe

    Code:
    #include <cstdlib>
    #include <iostream>
    
    roll();
    
    int main(int argc, char *argv[]) {
      ...
      int roll1;
      roll1 = roll();
      ...
    }
    
    roll() {
      return(rand() % num2 + 1);
    }

  • #3
    New to the CF scene
    Join Date
    Jul 2007
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation

    Ok... Did all of that, but I'm still getting the same error message, that "total" is undeclared. For clarification's sake, this is meant to be a die rolling program.

  • #4
    Gox
    Gox is offline
    Regular Coder Gox's Avatar
    Join Date
    May 2006
    Location
    Ontario, Canada
    Posts
    392
    Thanks
    2
    Thanked 20 Times in 20 Posts
    Post your code as it currently stands. If the compiler error says that total is undeclared then chances are good you didn't declare that variable.

    Also, in your initial code I noticed you were using goto's. Goto's are now considered to be poor coding style. Yes, there are still some people that will argue that they can be useful, but most will argue that they are messy and not needed if the logic is correct.

    EDIT: Hopefully my statement about goto's doesn't start the age old debate again...
    Last edited by Gox; 07-07-2007 at 04:19 AM.

  • #5
    New to the CF scene
    Join Date
    Jul 2007
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation

    Ok...current code:

    ___________________________________________________________

    #include <cstdlib>
    #include <iostream>

    using namespace std;

    int num1, num2;
    int roll(){
    return(rand() % num2 + 1);
    }

    int main(int argc, char *argv[])
    {

    cout << "How many?";
    cin >> num1;
    cout << "Sides?";
    cin >> num2;
    start:
    {
    num1 = num1-1;
    int roll1;
    roll1 = roll();
    int total;
    total = total + roll1;
    if
    (num1>0); goto start;
    goto endgame;
    }
    endgame:
    {
    cout << "Total equals" << total;}
    system("PAUSE");
    return EXIT_SUCCESS;
    }

    ___________________________________________________________

    Note: the "total" message highlights the third line from the bottom, and says that the "total" called for there is undeclared. It is the only error message I'm getting. So, while my code may look horrendous, I think one error message on my first attempt isn't half bad.

  • #6
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    int num1, num2; // these don't have to be global and shouldn't be
                    // the proper scope for both is the same as total
    int roll(/* int num2 */) // take a parameter to obviate the need for global num2
    {
    	return(rand() % num2 + 1);
    }
    
    int main(int argc, char *argv[])
    {
    	// int num1, num2;
    	cout << "How many?";
    	cin >> num1;
    	cout << "Sides?";
    	cin >> num2;
    	int total = 0; // Declare in an outer scope to live on outside braces
    	               // Initialize variables! Not setting this to 0 causes the final outcome
    		       // to be independent of the work you do with this variable.
    	start:
    	{ // this block is starting a new scope
    		num1 = num1-1; // or: --num1; or: num1 -= 1;
    		int roll1; // or: int roll1 = roll(); and get rid of the next line
    		roll1 = roll();
    		// int total;  wrong, will be destroyed before it can be printed
    		total = total + roll1;
    		if (num1>0)/* ; */ goto start; // first semicolon was wrong; also goto would be better as a loop
    		goto endgame;  // ok, but doesn't make any sense since endgame is next up anyway
    	} // scope is over, all automatic variables declared within are no longer valid
    	endgame: // redundant
    	{ 
    		cout << "Total equals" << total;
    	}   
    
    	system("PAUSE"); // ok, but not portable ;]
    	return EXIT_SUCCESS;
    }

  • #7
    New to the CF scene
    Join Date
    Jul 2007
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation

    Thanks. Yes, I know my code is hideous-looking more than likely, but it works fine now.

  • #8
    New Coder
    Join Date
    Jul 2007
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by rain ghost View Post
    Ok...current code:

    ___________________________________________________________

    #include <cstdlib>
    #include <iostream>

    using namespace std;

    int num1, num2;
    int roll(){
    return(rand() % num2 + 1);
    }

    int main(int argc, char *argv[])
    {

    cout << "How many?";
    cin >> num1;
    cout << "Sides?";
    cin >> num2;
    start:
    {
    num1 = num1-1;
    int roll1;
    roll1 = roll();
    int total;
    total = total + roll1;
    if
    (num1>0); goto start;
    goto endgame;
    }
    endgame:
    {
    cout << "Total equals" << total;}
    system("PAUSE");
    return EXIT_SUCCESS;
    }

    ___________________________________________________________

    Note: the "total" message highlights the third line from the bottom, and says that the "total" called for there is undeclared. It is the only error message I'm getting. So, while my code may look horrendous, I think one error message on my first attempt isn't half bad.

    If I may...

    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    int num1, num2;
    int roll(){
      return(rand() % num2 + 1);
    }
    
    int main(int argc, char *argv[])
    {
      
      cout << "How many?";
      cin >> num1;
      cout << "Sides?";
      cin >> num2;
      start:
      {
        num1 = num1-1;
        int roll1;
        roll1 = roll();
        int total;
        total = total + roll1;
        if 
        (num1>0); goto start;
        goto endgame;
      }
      endgame:
      {
        cout << "Total equals" << total;
      }
      system("PAUSE");
      return EXIT_SUCCESS;
    }
    I just couldn't read you code without increements in it. Now you se that total is not declared in the same scope as the line cout << "Total equals" << total; is in, I don't really know how good the compiler is to spot the order they will happen in time. I mean, the variable call could come before the declaration of the variable. Not good. Besides, when you use goto start;, the line where total is declared will run again and you cannot have it in that way. The compiler must know that the variable is going to be declared only once in each function call, and that it is going to be declared before the calls to the variable.

    Even roll's declarations needs to be moved, as you se they do best in standing somewhere between the function head and start:

    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    int num1, num2;
    int roll(){
      return(rand() % num2 + 1);
    }
    
    int main(int argc, char *argv[])
    {
      int roll1;
      int total;
      
      cout << "How many?";
      cin >> num1;
      cout << "Sides?";
      cin >> num2;
      start:
      {
        num1 = num1-1;
        roll1 = roll();
        total = total + roll1;
        if (num1>0); goto start;
        goto endgame;
      }
      endgame:
      {
        cout << "Total equals" << total;
      }
      system("PAUSE");
      return EXIT_SUCCESS;
    }
    Last edited by TriKri; 07-07-2007 at 11:35 AM.

  • #9
    Super Moderator sage45's Avatar
    Join Date
    May 2002
    Posts
    1,060
    Thanks
    0
    Thanked 13 Times in 13 Posts
    I agree with ralph. Your goto would serve you better as a loop.

    -sage-
    HTML & CSS Forum Moderator

    "If you don't know what you think you know, then what do you know."
    R.I.P. Derrick Thomas #58
    1/1/1967 - 2/8/2000


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •