Hello I am new here. can someone explain how to fix a segmentation fault

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2008
Posts: 10
Reputation: dpfaff is an unknown quantity at this point 
Solved Threads: 0
dpfaff dpfaff is offline Offline
Newbie Poster

Hello I am new here. can someone explain how to fix a segmentation fault

 
0
  #1
Apr 15th, 2008
I am taking a introduction to C++ programming class in college and I am having some issues with my assignment. If anyone can help It would mean a world of diffrence to me. Thanks in advance.

Okay the assigenment is to write a program that simulates the lottery, the program should have an array of five integers named lottery and should generate a random number in the range of 0 through 9 for each element in the array. The user should enter five digits which should be stored in an integer array named user. The program is to compare the corresponding elements in the two arrays and keep a count of the digits that match.

The program should then display the random numbers stored in the lottery array and the number of digits matching digits. if numbers of digits match display a prize pakagem of each coresspoding to 2, 3,4 and all five digits that are matching.

I have gotten the code to compile and it runs though the prompts asking the user to enter there numbers, but when I run the progam I keep getting a "segmentation fault" . How can I fix this?

below is my code

---------------------------------------------------------------------------------------------------------

  1. // Description: This program allows the user to enter five numbers, like a lottery.
  2. // The program chooses five numbers at random and compares them to see if the user won.
  3. // Inputs: numbers
  4. // Outputs: random numbers, prize message, user numbers
  5. // Programmer Name: David Pfaff
  6. // Username: dpfaff
  7. // Date: April 9, 2008
  8.  
  9. #include <iostream>
  10. #include <iomanip>
  11. #include <ctime>
  12. #include <cstdlib>
  13. using namespace std;
  14.  
  15. const int MAXCHARS = 5;
  16.  
  17. // prints a number of new lines
  18. // inputs: num, the number of new lines desired
  19. // outputs: num newlines on the screen
  20. // usage: printNewLines(4);
  21. void printNewLines(int num);
  22.  
  23. // displays the programmer name and description
  24. // outputs: the programmer name and description of program
  25. // usage: displayInfo();
  26. void displayInfo();
  27.  
  28. // allows the user to select five numbers to play the lottery.
  29. // inputs: five numbers
  30. // outputs: none
  31. // usage: getTicket (int user[])
  32. void getTicket (int user[]);
  33.  
  34. // the computer selects five random numbers for the lottery
  35. // inputs: none
  36. // outputs: five random numbers
  37. // usage: getLottery(int lottery[])
  38. void getLottery(int lottery[]);
  39.  
  40. // compares the user input and the computer's output
  41. // inputs: none
  42. // outputs: number of pairs
  43. // usage: int compare(user, lottery)
  44. int compare(int user[], int lottery[]);
  45.  
  46. // tells the user which prize package he/she has won if there were any matching pairs
  47. // inputs: none
  48. // outputs: prize package
  49. // usage: displayPrize(user)
  50. void displayPrize(int correct);
  51.  
  52. // receives an answer from user as to whether he/she would like to continue playing
  53. // inputs: answer
  54. // ouputs: repeat of program
  55. // usage: toContinue(answer)
  56. void toContinue(char& answer);
  57.  
  58. int main()
  59. {
  60. int user[MAXCHARS];
  61. int lottery[MAXCHARS];
  62. int correct, compareNumbers;
  63. char answer;
  64.  
  65. do
  66. {
  67. printNewLines(2);
  68. displayInfo();
  69. printNewLines(2);
  70.  
  71. getTicket(user);
  72. printNewLines(2);
  73.  
  74. getLottery(lottery);
  75. printNewLines(2);
  76.  
  77. correct = compareNumbers[user, lottery];
  78. printNewLines(2);
  79.  
  80. displayPrize(correct);
  81. printNewLines(2);
  82.  
  83. toContinue(answer);
  84. printNewLines(2);
  85. }while(answer == 'y' || answer == 'Y');
  86. }
  87.  
  88.  
  89. // prints a number of new lines
  90. // inputs: num, the number of new lines desired
  91. // outputs: num newlines on the screen
  92. // usage: printNewLines(4);
  93. void printNewLines(int num)
  94. {
  95. for (int i = 0; i < num; i++)
  96. cout << endl;
  97. }
  98.  
  99. // displays the programmer name and description
  100. // outputs: the programmer name and description of program
  101. // usage: displayInfo();
  102. void displayInfo()
  103. {
  104. cout << "David Pfaff";
  105. printNewLines(2);
  106. cout << "Description: This program allows the user to enter five numbers, like a lottery.";
  107. cout << "The program chooses 5 numbers at random and compares them to see if the user won.";
  108. }
  109.  
  110. // allows the user to select five numbers to play the lottery.
  111. // inputs: five numbers
  112. // outputs: none
  113. // usage: getTicket (int user[])
  114. void getTicket (int user[])
  115. {
  116. cout << "Enter a single digit number (0 through 9).";
  117. printNewLines(2);
  118.  
  119. for(int i = 0; i < MAXCHARS; i++)
  120. {
  121. cout << "Enter number --> ";
  122. cin >> user[i];
  123.  
  124. if(user[i] < 0 || user [i] > 9)
  125. {
  126. cout << "Please enter another number, your entry was invalid.";
  127. printNewLines(2);
  128. cout << "Enter a number -> ";
  129. cin >> user[i];
  130. printNewLines(2);
  131. }
  132. }
  133. }
  134.  
  135. // the computer selects five random numbers for the lottery
  136. // inputs: none
  137. // outputs: five random numbers
  138. // usage: getLottery(int lottery[])
  139. void getLottery(int lottery[])
  140. {
  141. for (int num = 0; num < MAXCHARS; num++)
  142. lottery[num] = rand() % 9;
  143. }
  144.  
  145. // compares the user input and the computer's output
  146. // inputs: none
  147. // outputs: number of pairs
  148. // usage: int compare(user, lottery)
  149. int compare (int user[], int lottery[])
  150. {
  151. int numberCorrect = 0;
  152. int index = 0;
  153. while (index < MAXCHARS)
  154. {
  155. if(lottery[index] == user[index])
  156. numberCorrect++;
  157. }
  158. return numberCorrect;
  159. }
  160.  
  161. // displays the random numbers and the user's numbers
  162. // inputs: none
  163. // outputs: random numbers, user numbers
  164. // usage: displayNumbers(lottery, user)
  165. void displayNumbers(int lottery[], int user[])
  166. {
  167. cout << "Lottery numbers are -> ";
  168. for(int count = 0; count < MAXCHARS; count++)
  169. cout << lottery[count];
  170.  
  171. cout << "User numbers are -> ";
  172. for(int i = 0; i < MAXCHARS; i++)
  173. cout << user[i];
  174. }
  175.  
  176. // tells the user which prize package he/she has won if there were any matching pairs
  177. // inputs: none
  178. // outputs: prize package
  179. // usage: displayPrize(user)
  180. void displayPrize(int correct)
  181. {
  182. switch(correct)
  183. {
  184. case 0: cout << "You did not match any numbers, please play again." << endl;
  185. break;
  186. case 1: cout << "You only matched one number, that is not enough to win." << endl;
  187. break;
  188. case 2: cout << "You matched 2 numbers, you win $100." << endl;
  189. break;
  190. case 3: cout << "You matched 3 numbers, you win $1000." << endl;
  191. break;
  192. case 4: cout << "You matched 4 numbers, you win $100,000." << endl;
  193. break;
  194. case 5: cout << "You matched all the numbers, you win $2,000,000." << endl;
  195. break;
  196. }
  197. }
  198. // receives an answer from user as to whether he/she would like to continue playing
  199. // inputs: answer
  200. // ouputs: repeat of program
  201. // usage: toContinue(answer)
  202. void toContinue(char& answer)
  203. {
  204. cout << "Would you like to play again? If so," << endl;
  205. cout << "please enter a 'y' for yes and an 'n' for no." << endl;
  206. cout << "Press enter after answering.";
  207. printNewLines(2);
  208. cin >> answer;
  209. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,511
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1480
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Hello I am new here. can someone explain how to fix a segmentation fault

 
0
  #2
Apr 15th, 2008
>>correct = compareNumbers[user, lottery];

First, compareNumbers is uninitialized, which is why you get the core dump.

Second, what exactly is that line doing? compareNumbers is a simple integer, so how can you possibly use it like that ???? user and lottery are both int arrays. That doesn't make sense, but the compiler doesn't complain.

I've never seen anything like that before -- what is it doing?
Last edited by Ancient Dragon; Apr 15th, 2008 at 10:22 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 10
Reputation: dpfaff is an unknown quantity at this point 
Solved Threads: 0
dpfaff dpfaff is offline Offline
Newbie Poster

Re: Hello I am new here. can someone explain how to fix a segmentation fault

 
0
  #3
Apr 15th, 2008
I corrected

int correct, compareNumbers = 0;

but after it gose through once and asks I want to play agin, Its goes iinto an infinite loop.

why is it doing this?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,511
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1480
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Hello I am new here. can someone explain how to fix a segmentation fault

 
0
  #4
Apr 15th, 2008
learn to use you compiler's debugger and step through the program one line at a time so that you can see what its doing.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 10
Reputation: dpfaff is an unknown quantity at this point 
Solved Threads: 0
dpfaff dpfaff is offline Offline
Newbie Poster

Re: Hello I am new here. can someone explain how to fix a segmentation fault

 
0
  #5
Apr 16th, 2008
Hey It works!!! It's so nice when a computer does what you tell it to.

Thanks for your help.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,511
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1480
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Hello I am new here. can someone explain how to fix a segmentation fault

 
0
  #6
Apr 16th, 2008
Originally Posted by dpfaff View Post
Hey It works!!! It's so nice when a computer does what you tell it to.

Thanks for your help.
The computer was doing exactly what you told it to do. Your problem was that you told it to do the wrong thing
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC