GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 08-May-2004, 17:18
Shufty Shufty is offline
New Member
 
Join Date: May 2004
Posts: 14
Shufty is on a distinguished road

Problem that doesn't make sense! Please help!


Hi, I'm having a problem with this code. As far as I know it should work fine, but it takes issue with some code in one function. When I compiled this outside of the original program it was fine. Any help would be brilliant! I've underlined the bit that causes the problems
CPP / C++ / C Code:
#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int accum = 0;    //global integer  (used in verification)
int numbers();    //declaration of function to select numbers
int selection[5]; //declaration of aaray for number storage
int confirm();    //declaration of function
int generate();   //declaration of function
int scenario();   //declaration of function
int winning[5];   //declaration of array
int verify(); //declaration of function




//number select function//
int numbers ()
    {
    cout << "Select your first number\n";      //enter first number
    cin >> selection[0];
    cout << "Select your second number\n";    //enter second number
    cin >> selection[1];
    cout << "Select your third number\n";     //enter third number
          cin >> selection[2];
    cout << "Select your fourth number\n";    //enter fourth number
           cin >> selection[3];
     cout << "Select your fifth number\n";     //enter fifth number
           cin >> selection[4];
     cout << "Select your sixth number\n";     //enter sixth number
          cin >> selection[5];

           }

              //confirmation function
          int confirm ()
           {
           int confirmchoice;   //stores choice made by user
           cout << "Are you sure? Please enter 1 for yes and 0 for No\n";
           cin >> confirmchoice;
               if (confirmchoice == 0)
                   numbers(); //recalls numbers function to re-select numbers
                   }

                        //random number generation function
                        int generate ()
                        {
                        const int n = 5;
                        for ( int i =0; i<n; i++ )
                        {
                        winning[i]=(rand()%49)+1;//
                        cout<<winning[i]<<endl;
                          }

                         }

                             [u] int verify ()
                              {
                         const int n = 5;
                                int m = 5;
                          for ( int i = 0; i <= n; i++)
                           {
                              for (int j = 0; j <= m; j++)
                              {
                            if(winning[i] == selection[j])
                            {

                             acumm++;
                                       }
                                        }
                               }[/u]
                          //switch function to determine player's winnings (if any)
                          int scenario ()
                          {  switch(accum)
                              {
                          case 3:
                          cout << "Well done! You've won £10!\n";
                          break;

                          case 4:
                          cout << "You have won £5000!\n";
                          break;

                          case 5:
                          cout << "You have won £250,000!\n";
                          break;

                          case 6:
                          cout << "You've won the jackpot! £1,000,000!!! Congratulations!\n";
                          break;

                          default:
                          cout << "Sorry you haven't won, better luck next time!\n";
                          break;
                                 }
                          }

 void main()
{

  numbers();
  confirm();
  generate();


}
Last edited by dsmith : 08-May-2004 at 20:00. Reason: Please use [c] & [/c] for syntax highlighting
  #2  
Old 08-May-2004, 18:09
machinated machinated is offline
Regular Member
 
Join Date: Mar 2004
Location: victoria, canada
Posts: 324
machinated has a spectacular aura aboutmachinated has a spectacular aura about
none of your return functions have return statements in them. Every return function must have atleast one return statement in it.
  #3  
Old 08-May-2004, 20:10
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Hi Shufty. I am not sure if this is the cause of your problem, but it can't help

You declare section as a 5 element array.
CPP / C++ / C Code:
int selection[5]; //declaration of aaray for number storage
In C, arrays are 0-indexed, so you have an array of 0 to 4 items.

However, here in your number function, you are placing 6 elements into the array, so you have gone past your bounds:
CPP / C++ / C Code:
//number select function//
int numbers ()
    {
    cout << "Select your first number\n";      //enter first number
    cin >> selection[0];
    cout << "Select your second number\n";    //enter second number
    cin >> selection[1];
    cout << "Select your third number\n";     //enter third number
          cin >> selection[2];
    cout << "Select your fourth number\n";    //enter fourth number
           cin >> selection[3];
     cout << "Select your fifth number\n";     //enter fifth number
           cin >> selection[4];
     cout << "Select your sixth number\n";     //enter sixth number
          cin >> selection[5];

}

In addition later, you go back to a 5-element array with your for statements:
CPP / C++ / C Code:
                      const int n = 5;
                        for ( int i =0; i<n; i++ )
                        {
                        winning[i]=(rand()%49)+1;//
                        cout<<winning[i]<<endl;
                          }


In this case, the for statement loops from 0 to 4. This is correct with your original decleration, but is short with your numbers routine.

One last thing. I am not sure why you are using const int n=5 for your for-statements, you could simply put:
CPP / C++ / C Code:
for ( int i =0; i<5; i++ )

You should fix the index overrun either by defining the array as a six-element array and change your for statements or change your number routine to only input 5 numbers. If you are still having problems, let us know.

Good Luck,
d
  #4  
Old 09-May-2004, 07:29
Shufty Shufty is offline
New Member
 
Join Date: May 2004
Posts: 14
Shufty is on a distinguished road

Another day, yet another problem


Hi again. There's no errors being shown when I compile now BUT the console screen flashes up briefly and disappears and does not do what its supposed to at all. All I can see is it showing some numbers and a sorry message. So for some reason it is skipping the first function I've told it to run in main. I think it may be related to a while statement I added to the numbers function. Here's the latest version of the code (thanks again for everyone's help, I greatly appreciate it!!:-
CPP / C++ / C Code:
#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int accum = 0;    //global integer  (used in verification)
int numbers();    //declaration of function to select numbers
int selection[5]; //declaration of aaray for number storage
int generate();   //declaration of function
int scenario();   //declaration of function
int winning[5];   //declaration of array
int verify();     //declaration of function

 int main()
{

  numbers();
  generate();
  verify();
  scenario();
         return 0;
}


//number select function//
int numbers ()
{
   int confirmchoice = 1;

     while (confirmchoice == 0)
     {
    cout << "Select your first number\n";      //enter first number
    cin >> selection[0];
    cout << "Select your second number\n";    //enter second number
    cin >> selection[1];
    cout << "Select your third number\n";     //enter third number
          cin >> selection[2];
    cout << "Select your fourth number\n";    //enter fourth number
           cin >> selection[3];
     cout << "Select your fifth number\n";     //enter fifth number
           cin >> selection[4];
     cout << "Select your sixth number\n";     //enter sixth number
          cin >> selection[5];

             cout << "Are you sure? Please enter 1 for yes and 0 for No\n";//confirmation
           cin >> confirmchoice;
      }
}


                        //random number generation function
                        int generate ()
                        {
                        const int n = 5;
                        for ( int i =0; i<n; i++ )
                        {
                        winning[i]=(rand()%49)+1; //generates a random number between 1 and 49
                        cout<<winning[i];

                          }

                         }

  int verify ()
  {
   const int n = 5;
    int m = 5;
     for ( int i = 0; i <= n; i++)
      {
        for (int j = 0; j <= m; j++)
       {
         if(winning[i] == selection[j])
          {
           int acumm;
           acumm++;
            }
           }
         }
           }

                          //switch function to determine player's winnings (if any)
                          int scenario ()
                          {  switch(accum)
                              {
                          case 3:
                          cout << "Well done! You've won £10!\n";
                          break;

                          case 4:
                          cout << "You have won £5000!\n";
                          break;

                          case 5:
                          cout << "You have won £250,000!\n";
                          break;

                          case 6:
                          cout << "You've won the jackpot! £1,000,000!!! Congratulations!\n";
                          break;

                          default:
                          cout << "Sorry you haven't won, better luck next time!\n";
                          break;
                                 }
                          }
Last edited by dsmith : 09-May-2004 at 08:23. Reason: Please use [c] & [/c] for syntax highlighting
  #5  
Old 09-May-2004, 07:35
Shufty Shufty is offline
New Member
 
Join Date: May 2004
Posts: 14
Shufty is on a distinguished road
Quote:
Originally Posted by dsmith
Hi Shufty. I am not sure if this is the cause of your problem, but it can't help

You declare section as a 5 element array.
CPP / C++ / C Code:
int selection[5]; //declaration of aaray for number storage
In C, arrays are 0-indexed, so you have an array of 0 to 4 items.

However, here in your number function, you are placing 6 elements into the array, so you have gone past your bounds:
You should fix the index overrun either by defining the array as a six-element array and change your for statements or change your number routine to only input 5 numbers. If you are still having problems, let us know.

Good Luck,
d

Hi, I need 6 numbers in there, so doesn't using [5] work? Seen as its going 0,1,2,3,4,5 which is six places. Sorry if this sounds stupid but I am very new to all this!
  #6  
Old 09-May-2004, 08:15
Shufty Shufty is offline
New Member
 
Join Date: May 2004
Posts: 14
Shufty is on a distinguished road
ok I've sorted that problem. I declared confirmchoice as 1 which conflcited with the while statement (always one stupid little thing! ) But now it won't go onto the next function from main. Any ideas?
  #7  
Old 09-May-2004, 08:18
machinated machinated is offline
Regular Member
 
Join Date: Mar 2004
Location: victoria, canada
Posts: 324
machinated has a spectacular aura aboutmachinated has a spectacular aura about
you need to change the while loop to do{}while(); this way it will enter the loop. currently your function is not entering while loop at all. second, keep in mind that when you declare an array, you declare it with a size: int array[5];
5 is its size. so it can store 5 elements. array storage in c/c++ starts with 0, so your array will store from array[0] to array[4], which is 5 elements. declaration is different than access. if you want 6 elements declare it as array[6], altho the last element will be array[5], if you try to access the 7th element or array[6], the compiler or system won't stop you in most cases but will give you whatever resides there, which will be garbage. so you have to make sure to pay attention.
  #8  
Old 09-May-2004, 08:21
machinated machinated is offline
Regular Member
 
Join Date: Mar 2004
Location: victoria, canada
Posts: 324
machinated has a spectacular aura aboutmachinated has a spectacular aura about
you still haven't added return statements to your function. either add return statements or change your functions to void. also in order to use the rand() function properly you need to seed the random values first using srand(time(NULL)); otherwise it'll always come up with the same values.
  #9  
Old 09-May-2004, 08:31
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Quote:
Originally Posted by Shufty
ok I've sorted that problem. I declared confirmchoice as 1 which conflcited with the while statement (always one stupid little thing! ) But now it won't go onto the next function from main. Any ideas?

Hi Shufty. When you post code could you please enclose it in [c] and [/c] tags? It really helps to see the code better when it has the proper syntax highlighting.

Onto your code. You properly changed your for statements, but you should also declare winning & selection as six element arrays not five.

Also, what happens when you run your program? You say it doesn't go on to the next function. How do you know that? The next function is generate which has not i/o to screen.
  #10  
Old 09-May-2004, 09:00
machinated machinated is offline
Regular Member
 
Join Date: Mar 2004
Location: victoria, canada
Posts: 324
machinated has a spectacular aura aboutmachinated has a spectacular aura about
Quote:
Originally Posted by dsmith

Also, what happens when you run your program? You say it doesn't go on to the next function. How do you know that? The next function is generate which has not i/o to screen.

generate has output to screen.

also inside your verify function, inside the if statement inside the nested for loops, you declare int accum; accum has already been declared globally. declaring it inside the loops is just assigning it garbage values each time and your function won't perform like it's supposed to at all. also change your int functions to type void if you are not planning on returning anything.
 
 

Recent GIDBlogToyota - 2008 November Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Yet another CD burner problem: Lite-On LSC-24082K Erwin Computer Hardware Forum 1 22-May-2004 12:28
Another FX 5600 problem (but with details that might shed light on this) BobDaDuck Computer Hardware Forum 2 16-Apr-2004 08:53
Linux Kernel Upgrade Mini Howto dsmith Computer Software Forum - Linux 3 05-Apr-2004 23:10
problem with php5 cgi installation fab13 Apache Web Server Forum 3 19-Nov-2003 10:11
CD Buring Failed skanth2000 Computer Hardware Forum 1 15-Nov-2003 04:52

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 02:00.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.