Answer wont display right
Author |
Message |
JR
|
Posted: Tue Oct 19, 2004 10:27 pm Post subject: Answer wont display right |
|
|
this program calculates the points the user got from the questions, well a part of it. now when i putput the points i get some strange number which is wrong, how do i make it output the right nunbmer at the end of the 5 questions when it calculated all the points?
code: |
// Alex R
// A5
// October 10th
#include <iostream.h>
#include <stdlib.h>
#include <time.h>
/*
Name: Ran
Purpose: Generate a random number
Gets: A int variable
Retrun: num which is the random number
*/
int ran(int &num)
{
num= (rand() % 100)+1; //Gets a random number from 1-100
return (num);
}
/*
Name: add
Purpose: adds 2 generated random numbers
Gets: 2 numbers(variables)
Retrun: the addition between the numbers
*/
int add (int &a, int &b)
{
int total=0;
ran(a);
ran(b);
total = a + b;
return (a+b);
}
/*
Name: subtract
Purpose: subtracts 2 generated random numbers
Gets: 2 variables
Retrun: subtraction of 2 numbers
*/
int subtract (int &a, int &b)
{
int total=0;
ran(a);
ran(b);
total = a - b;
return (a-b);
}
/*
Name: multiply
Purpose: multiplies 2 generated random numbers
Gets: 2 variables
Retrun: multiplication of 2 numbers
*/
int multiply (int &a, int &b)
{
int total=0;
ran(a);
ran(b);
total = a * b;
return (a*b);
}
/*
Name: answers
Purpose: checks the answers
Gets: one variable
Retrun: N/A
*/
void answers( int &total){
// 10 points if answered correctly the first time.
// 5 points if answered correctly the second time.
// 2 points three times a charm.
// 0 points if you struck out!
int answer;
int wrong=0;
int points;
for(int i=0; i < 3; i++){
cin>>answer;
if( answer != total ){
if (i < 2){
cout<<"WRONG, Try again."<<endl;
wrong++;
}
if( i == 2){
cout<<"You have missed 3 times. The answer is "<<total<<endl;
points+=0;
}
}else if ( answer == total ){
cout<<"CORRECT"<<endl;
if (wrong == 0 ){
points+=10;
}else if ( wrong == 1){
points+=5;
}else if (wrong == 2){
points+=2;
}
break;
}
}
cout<<"You have "<<points<<" points \n";
}
/*
Name: menu
Purpose: Outputs the menu
Gets: 2 variables
Retrun: N/A
*/
void Menu(int &a, int &b )
{
//int x;
int total, choice;
cout<<endl;
cout<<"Chose one of the following.\n";
cout<<"1. Addition\n2. Subtraction\n3. Multiplication\n4. Exit\n";
cin>>choice;
switch (choice){
case 1: add(a, b);
cout<<a<<" + "<<b<<" = "<<endl;
total=a+b;
answers(total);
break;
case 2: subtract(a, b);
cout<<a<<" - "<<b<<" = "<<endl;
total=a-b;
answers(total);
break;
case 3: multiply(a, b);
cout<<a<<" * "<<b<<" = "<<endl;
total=a*b;
answers(total);
break;
case 4: cout<<"Bye!!!" ;
break;
default: cout<<"The options are only 1-4.";
break;
}
}
int main ()
{
srand(time(0));
int a, b;
// We can only have 5 problems.
for ( int i=0; i < 5; i++){
Menu( a, b );
}
cin.get();
cin.get();
return 0;
}
|
|
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Andy
|
Posted: Wed Oct 20, 2004 3:02 pm Post subject: (No subject) |
|
|
u have to define points to 0... if u dont, the program will just give you what ever is at that specific memory allocation at that time |
|
|
|
|
![](images/spacer.gif) |
JR
|
Posted: Thu Oct 21, 2004 6:14 am Post subject: (No subject) |
|
|
lol i misseds that one thx |
|
|
|
|
![](images/spacer.gif) |
JHanson90
![](http://home.comcast.net/~joshuahanson/shock_100x100.png)
|
Posted: Fri Oct 22, 2004 11:20 pm Post subject: (No subject) |
|
|
Since the title of this topic applies to the problem that I'm having, I might as well post this in here.
I'm learning C (note for the careless reader: not C++), and I'm in the second chapter of a book about it. My exercise is to write a program that will ask the user for a number, and the program returns all of that number's factors. It compiles fine. But at run-time, I encounter an error that reads "Floating point exception" when it gets to the 'for' loop part. I tried to Google it, but I couldn't understand much of what was said being a noob to C. Here's the program: code: | /* Chapter 2: Section 4, Exercise 3 */
#include <stdio.h>
int main (void)
{
/* Tell the user what this program does. */
printf ("This program will first ask you for an integer. It will then give you all of the factors of that integer.\n");
/*
Ask for the integer, and put it into the input variable.
*/
int input;
printf ("Please input any integer: ");
scanf ("%d", &input);
int i;
printf ("Here are the factors of your input (%d).\n", input);
for (i = 0; i < input; i++)
{
if (input / i) /* If variable input divided by variable i is any other number than 0, execute the following. */
{
/*
If there are no more factors left to bother with , use a period instead of another comma
(e.g. it's pointless to try to see if 20 / 11 will work, because I know there won't be any
factors larger than half of the input).
*/
if ((i * 2) > input)
printf ("%d.\n", i);
else {
printf ("%d, ", i);
}
}
}
return 0;
} | I tried replacing the '(input / i)' with '(input % i)', and a few other things, just to see if it would at least fix the error regardless of the task at hand, but it always gave me the runtime error unless I commented out that if statement. |
|
|
|
|
![](images/spacer.gif) |
wtd
|
Posted: Sat Oct 23, 2004 3:35 am Post subject: (No subject) |
|
|
Which compiler are you using?
code: | for (i = 0; i < input; i++) |
When you divide by i, the first time through the loop, you're dividing by zero. Division by zero does bad bad things.
Since 0 isn't a factor of anything, I suggest starting with:
code: | for (i = 1; i < input; i++) |
And a few other modifications:
code: | #include <stdio.h>
int main (void)
{
int input = 0, i;
fprintf(stdout, "This program will first ask you for an integer. It will then give you all of the factors of that integer.\n");
fprintf(stdout, "Please input any integer: ");
fscanf(stdin, "%d", &input);
fprintf(stdout, "Here are the factors of your input (%d).\n", input);
for (i = 1; i <= input; i++)
{
if (input % i == 0)
{
if (i == input)
printf("%d.\n", i);
else
printf("%d, ", i);
}
}
return 0;
}
|
|
|
|
|
|
![](images/spacer.gif) |
Andy
|
Posted: Sat Oct 23, 2004 9:55 am Post subject: (No subject) |
|
|
yea thats the problem.. btw, wtd what does %d do? im not too familiar wit C.. only c++ |
|
|
|
|
![](images/spacer.gif) |
wtd
|
|
|
|
![](images/spacer.gif) |
Andy
|
Posted: Sat Oct 23, 2004 5:11 pm Post subject: (No subject) |
|
|
OOooo... thats nice.. thx man |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
wtd
|
Posted: Sat Oct 23, 2004 5:50 pm Post subject: (No subject) |
|
|
Conveniently, those standards are implemented by many languages.
O'Caml:
code: | open Printf
include Printf
let a = 9 in
printf "%d\n" a |
Java 1.5/5.0:
code: | import java.lang.*;
import java.io.*;
public class PrintfTest {
public static void main(String[] args) {
final int A = 9;
System.out.printf("%d\n", A);
}
} |
Perl:
code: | my $a = 9;
printf "%d\n", $a; |
Ruby:
code: | a = 9
printf "%d\n", a |
|
|
|
|
|
![](images/spacer.gif) |
JHanson90
![](http://home.comcast.net/~joshuahanson/shock_100x100.png)
|
Posted: Sat Oct 23, 2004 11:00 pm Post subject: (No subject) |
|
|
wtd wrote: What compiler are you using? The GNU Compiler Collection (commonly abbreviated as "GCC").
wtd wrote: When you divide by i, the first time through the loop, you're dividing by zero. Division by zero does bad bad things. Ah, so really just a math error (dumb of me not to remember that you can't divide by 0). Thanks.
wtd wrote: And a few other modifications:
code: | #include <stdio.h>
int main (void)
{
int input = 0, i;
fprintf(stdout, "This program will first ask you for an integer. It will then give you all of the factors of that integer.\n");
fprintf(stdout, "Please input any integer: ");
fscanf(stdin, "%d", &input);
fprintf(stdout, "Here are the factors of your input (%d).\n", input);
for (i = 1; i <= input; i++)
{
if (input % i == 0)
{
if (i == input)
printf("%d.\n", i);
else
printf("%d, ", i);
}
}
return 0;
}
|
Hmm, are fprintf() and fscanf() more efficient or something? |
|
|
|
|
![](images/spacer.gif) |
wtd
|
Posted: Sat Oct 23, 2004 11:45 pm Post subject: (No subject) |
|
|
JHanson90 wrote: Hmm, are fprintf() and fscanf() more efficient or something?
Don't know... Just showing off. ![Wink Wink](http://compsci.ca/v3/images/smiles/icon_wink.gif) |
|
|
|
|
![](images/spacer.gif) |
JHanson90
![](http://home.comcast.net/~joshuahanson/shock_100x100.png)
|
Posted: Sun Oct 24, 2004 8:02 pm Post subject: (No subject) |
|
|
wtd wrote: JHanson90 wrote: Hmm, are fprintf() and fscanf() more efficient or something?
Don't know... Just showing off. ![Wink Wink](http://compsci.ca/v3/images/smiles/icon_wink.gif)
Yes, I suppose that that's the main job of a consultant. ![Smile Smile](images/smiles/icon_smile.gif) |
|
|
|
|
![](images/spacer.gif) |
wtd
|
Posted: Sun Oct 24, 2004 9:18 pm Post subject: (No subject) |
|
|
He begins to understand... |
|
|
|
|
![](images/spacer.gif) |
|
|