-
April 7th, 2004, 11:06 PM
#1
my program is blowing it
I am doing something wrong with these floating point arrays... it crashes when it trys to scanf the first value and store it in the value [0]
#include <stdio.h>
int get_values();
int print_values();
float values[5];
char stock_names[5][10];
int main ()
{
get_values();
print_values();
return (0);
}
int get_values()
{
printf("Please Enter a stock name: ");
scanf(" %s", stock_names[1]);
printf("\nPlease enter the daily stock average of stock 1: ");
scanf(" %f", values[0]);
printf("\n\nPlease Enter a stock name: ");
scanf(" %s", stock_names[2]);
printf("\nPlease enter the daily stock average of stock 2: ");
scanf(" %f", values[1]);
printf("\n\nPlease Enter a stock name: ");
scanf(" %s", stock_names[3]);
printf("\nPlease enter the daily stock average of stock 3: ");
scanf(" %f", values[2]);
printf("\n\nPlease Enter a stock name: ");
scanf(" %s", stock_names[4]);
printf("\nPlease enter the daily stock average of stock 4: ");
scanf(" %f", values[3]);
printf("\n\nPlease Enter a stock name: ");
scanf(" %s", stock_names[5]);
printf("\nPlease enter the daily stock average of stock 5: ");
scanf(" %f", values[4]);
return (0);
}
int print_values()
{
printf("%s \t%.2f\n", stock_names[1], values[0]);
printf("%s \t%.2f\n", stock_names[2], values[1]);
printf("%s \t%.2f\n", stock_names[3], values[2]);
printf("%s \t%.2f\n", stock_names[4], values[3]);
printf("%s \t%.2f\n", stock_names[5], values[4]);
return (0);
}
-
April 7th, 2004, 11:25 PM
#2
I think that stock_names has the problem there.
the array index starts with 0... so it should be :
int get_values()
{
printf("Please Enter a stock name: ");
scanf(" %s", stock_names[0]);
printf("\nPlease enter the daily stock average of stock 1: ");
scanf(" %f", values[0]);
printf("\n\nPlease Enter a stock name: ");
scanf(" %s", stock_names[1]);
printf("\nPlease enter the daily stock average of stock 2: ");
scanf(" %f", values[1]);
printf("\n\nPlease Enter a stock name: ");
scanf(" %s", stock_names[2]);
printf("\nPlease enter the daily stock average of stock 3: ");
scanf(" %f", values[2]);
printf("\n\nPlease Enter a stock name: ");
scanf(" %s", stock_names[3]);
printf("\nPlease enter the daily stock average of stock 4: ");
scanf(" %f", values[3]);
printf("\n\nPlease Enter a stock name: ");
scanf(" %s", stock_names[4]);
printf("\nPlease enter the daily stock average of stock 5: ");
scanf(" %f", values[4]);
return (0);
}
int print_values()
{
printf("%s \t%.2f\n", stock_names[0], values[0]);
printf("%s \t%.2f\n", stock_names[1], values[1]);
printf("%s \t%.2f\n", stock_names[2], values[2]);
printf("%s \t%.2f\n", stock_names[3], values[3]);
printf("%s \t%.2f\n", stock_names[4], values[4]);
return (0);
}
Come Join This Poll Where are we from? (Ultimate)
Knowing is not enough; we must apply. Willing is not enough; we must do. - Johann Wolfgang
An idle brain is the devil's workshop. - unknown
-
April 7th, 2004, 11:36 PM
#3
Changed it...
still c rashes as im entering the first into value
any other guesses?
-
April 7th, 2004, 11:42 PM
#4
i chaged this, and it works for me..
scanf(" %.2f", values[0]);
Come Join This Poll Where are we from? (Ultimate)
Knowing is not enough; we must apply. Willing is not enough; we must do. - Johann Wolfgang
An idle brain is the devil's workshop. - unknown
-
April 7th, 2004, 11:58 PM
#5
Allright i tried that...
and it works except... when i print out the elements of the value floating point array...
i get 0.00
every time
also i figured out i have to fflush(stdin); after each scanf
here look at my new code ... compile it... tell me what you think
#include <stdio.h>
int get_values();
int print_values();
float values[5];
char stock_names[5][25];
int main ()
{
get_values();
print_values();
return (0);
}
int get_values()
{
printf("Please Enter a stock name: (no spaces) ");
scanf(" %s", stock_names[0]);
printf("\nPlease enter the daily stock average of stock 1: ");
scanf(" %.2f", values[0]);
fflush(stdin);
printf("\n\nPlease Enter a stock name: (no spaces) ");
scanf(" %s", stock_names[1]);
printf("\nPlease enter the daily stock average of stock 2: ");
scanf(" %.2f", values[1]);
fflush(stdin);
printf("\n\nPlease Enter a stock name: (no spaces) ");
scanf(" %s", stock_names[2]);
printf("\nPlease enter the daily stock average of stock 3: ");
scanf(" %.2f", values[2]);
fflush(stdin);
printf("\n\nPlease Enter a stock name: (no spaces) ");
scanf(" %s", stock_names[3]);
printf("\nPlease enter the daily stock average of stock 4: ");
scanf(" %.2f", values[3]);
fflush(stdin);
printf("\n\nPlease Enter a stock name: (no spaces) ");
scanf(" %s", stock_names[4]);
printf("\nPlease enter the daily stock average of stock 5: ");
scanf(" %.2f", values[4]);
fflush(stdin);
return (0);
}
int print_values()
{
printf("%s \t%.2f\n", stock_names[0], values[0]);
printf("%s \t%.2f\n", stock_names[1], values[1]);
printf("%s \t%.2f\n", stock_names[2], values[2]);
printf("%s \t%.2f\n", stock_names[3], values[3]);
printf("%s \t%.2f\n", stock_names[4], values[4]);
return (0);
}
-
April 8th, 2004, 01:06 AM
#6
Ok. I got it. lets change your code like this.
printf("Please Enter a stock name: ");
scanf(" %s", stock_names[0]);
printf("\nPlease enter the daily stock average of stock 1: ");
scanf(" %f", &values[0]);
fflush(stdin);
Come Join This Poll Where are we from? (Ultimate)
Knowing is not enough; we must apply. Willing is not enough; we must do. - Johann Wolfgang
An idle brain is the devil's workshop. - unknown
-
April 8th, 2004, 07:39 AM
#7
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|