Thread: New to C++ and need assistance

  1. #1
    Registered User
    Join Date
    Jun 2015
    Posts
    2

    New to C++ and need assistance

    Hello, I am new to C++ and I am enjoying it very much when I am getting it! I wrote a program to generate random numbers like a lottery but i have a few quirks in it, hopefully someone can assist me in correcting. I have extensively searched to find an answer and cannot. been working on this for about two days.
    1. I don't understand why i need to add a star next to my for loop to insure the input number is under 10.
    2. That same loop only works for the first input not any others.
    3. My random number generator is producing 0,0,0,0,1493466176 every time. (or similar last number).
    when I run this on my downloaded compiler it does what is stated above, if i run this on ideoneit.com it just runs all lines (ie. tells me i need to enter a number between 0 and 9, 5 times, but doesn't allow me to enter) and spits out 5 very large numbers.

    Code:
    1. /* James Smith
    2. week5 assignment 1
    3. lottery generator*/
    4. #include <iostream>
    5. #include <cstdlib>
    6. #include <ctime>
    7. using namespace std;
    8. //function prototype
    9. double randomnum(int);
    10. int main ()
    11. {
    12. //declare variables
    13. int lottery[5], user[5];
    14. //introduce program
    15. cout << "Is today your lucky day?? Play the lottery and find out! \n" << endl;
    16. //get user numbers
    17. cout << "Please input your 5 lucky numbers between 0 and 9! \n";
    18. for (int i=0; i<5; i++)
    19. {
    20. cout <<"Lucky number"<< (i+1)<<": ";
    21. cin >> user[i];
    22. if (*user>10)// why is this making me add a star? also only works for the first input
    23. {
    24. cout <<"The number must be between 0 and 9, please reenter" << endl;
    25. cin >> user[i];
    26. }
    27. }
    28. //generate lottery numbers
    29. randomnum;
    30. //display lottery numbers
    31. for(int i=0; i<5; i++)
    32. {
    33. cout << lottery[i] <<" " << endl;
    34. }
    35. //declare if a winner or not
    36. for (int n=0; n<1; n++)
    37. {
    38. if (user[n] == lottery [n])
    39. cout << "You were right it was your lucky day!! Congratulation you matched all 5 numbers!!" << endl;
    40. else
    41. cout << "I'm sorry you did not match all 5 numbers please play again" << endl;
    42. }
    43. return 0;
    44. }
    45. double randomnum(int lottery)
    46. {
    47. int num[5];
    48. for (int i =0; i<5; i++)
    49. {
    50. //initialize random seed
    51. srand (time(0));
    52. // generate number
    53. return num[i] =rand()%9;//getting garbage numbers
    54. }
    55. }

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    2
    i just made a few changes trying to look into this for my function prototype, I don't know if it matters to you, but it didn't change anything.
    line 45 double random(int lottery[]);
    line 29 randomness(lottery);
    line 9 double random (int[]);

  3. #3
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    It's a good thing you wrote it all flat. Indentation just gets in the way. Keep up the good work.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    To be less cryptic, you need to indent your code properly and let the board handle the formatting. Copy and paste into notepad or some similar application that strips color coding before pasting it into the reply box.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Mar 2015
    Posts
    184
    Regarding 1: remember user is the name of an array. So comparing an entire array with an int 10 doesn't work. The name of an array acts like a pointer to the first element. So *user is just another way of saying user[1].

  6. #6
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Quote Originally Posted by Yarin View Post
    It's a good thing you wrote it all flat. Indentation just gets in the way. Keep up the good work.
    This made me actually laugh out loud.

    But I'm scared the OP won't get that it's sarcasm.

    OP, Yarin was being sarcastic. Indentation and whitespace really, really help make code readable.

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    if (*user>10)// why is this making me add a star? also only works for the first input
    It's not making you do that, per se. Using the dereference operator is the minimum amount of effort required to read a value from user, given that user is an array.

    In most contexts an array name will decay to a pointer to the first element (i.e. apart from the sizeof operator, & operator, and when the array in question is a string literal).

    So, typing *user is virtually the same as user[0]. The second form usually has the advantage of being more readable and it's easy to pick another part of the array, for ex. user[i]. Just like how you did for the rest of the program.
    Last edited by whiteflags; 06-29-2015 at 12:20 AM.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by jiggunjer View Post
    Regarding 1: remember user is the name of an array. So comparing an entire array with an int 10 doesn't work. The name of an array acts like a pointer to the first element. So *user is just another way of saying user[1].
    Correction: user[0].
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Mar 2015
    Posts
    184
    Doh, yea user[0]. I also recommend this post to read more on the matter: Are pointers and arrays equivalent in C? - Eli Bendersky's website

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. in need of a little assistance
    By unknown2007 in forum C Programming
    Replies: 8
    Last Post: 11-02-2011, 07:05 AM
  2. Need Assistance?
    By shewj in forum C Programming
    Replies: 8
    Last Post: 04-09-2008, 07:57 AM
  3. Hello! May I get some assistance?
    By Flipz in forum C Programming
    Replies: 3
    Last Post: 12-02-2007, 12:20 PM
  4. need assistance....
    By malooch in forum C Programming
    Replies: 1
    Last Post: 12-19-2006, 12:48 AM
  5. Need some more assistance
    By Thantos in forum Windows Programming
    Replies: 6
    Last Post: 08-14-2003, 12:13 PM

Tags for this Thread