由于某种原因,我无法在程序中打印出“ *”。我想打印*如果满足条件。条件是当天的降雨量是否大于平均降雨量。
在平均值栏中,我得到了奇怪的符号。我试图调试到小数点,并得到-112 ascii。我不明白,但我尝试研究!
我是C的新手,请谅解。就像2天前一样刚刚学习!!
这是我的代码:
//Assignment one 9/20/2018
//Anthony Lomaistro and David Luong
//luongd5@student.lasalle.edu
//lomaistroa1@student.lasalle.edu
#include <stdio.h>
main() {
int n = 0; //counters for the loops
int x = 0; // counter for the loops
int counter = 0; // counter whenever i need to keep track of increments
int days_input = 0; // how many days we are keeping track of
int number_of_days = 0;
double rainfall_input = 0;
double rainfall_amount = 0; // how much rainfall per day
double rainfall_average = 0; // average of rainfall
double rainfall_total = 0;
double rainfall_counter = 0; // count how many days it rained above the average
int correct = 0;
double rainfall_array[50];//array that contains the user input
char rainfall_condition[50]; // array that contains the *
double sum = 0;
double average = 0;
double percent_days = 0; //rainfall % above average
double valid = 0;
double valid2 = 0;
printf("Welcome to Lomaistro and Luong's Rainfall Program \n");
printf("How many days would you like to keep track of? Please enter a value between 1 and 50: #%d:", n + 1);
//printf(number_of_days);
while (valid == 0) {
scanf_s("%d", &number_of_days);
if ((number_of_days > 50) || (number_of_days <= 0)) { // come back to this, this doesnt do anytihng
printf("Invalid value, please enter in a day that is between 1 and 50 \n");
}
else {
valid = 1;
}
}
//getting the user to enter in the rainfall
for (x = 0; x < number_of_days; x = x + 1) {
valid2 = 0;
while (valid2 == 0) {
printf("Enter rainfall (in inches): ");
scanf_s("%lf", &rainfall_amount);
if ((rainfall_amount >= 0) && (rainfall_amount <= 10)) {
valid2 = 1;
rainfall_array[x] = rainfall_amount;
}
else
printf("Please enter in a valid rainfall amount between 1 and 10");
}
}
//computing average
for (n = 0; n < number_of_days; n = n + 1) {
sum += rainfall_array[n];
average = sum / number_of_days;
}
printf("Mean daily rainfall(in inches): %lf", average);
//seeing if the * should be the array or not
for (n = 0; n < number_of_days; n = n + 1) {
if (rainfall_array[n] > average) {
rainfall_condition[n] = "*";
rainfall_counter = rainfall_counter + 1;
}
else
rainfall_condition[n] = "empty";
}
// print out the thing
printf("\n Days \t Amount \t >Mean \n");
printf("==============================\n");
for (n = 0; n < number_of_days; n = n + 1) {
printf("%d \t %f \t %c \n", n + 1, rainfall_array[n], rainfall_condition[n]);
}
percent_days = rainfall_counter / number_of_days;
percent_days = percent_days * 100;
printf("Number of days that rained above average : %f \n", rainfall_counter);
printf("Percentage of days that rained above average: %f%% \n", percent_days);
system("pause");
}
答案 0 :(得分:3)
rainfall_condition
是char
的数组,但是使用"*"
时要在其中放置指向字符串文字的指针。请将'*'
用作字符文字。更具体地说,这一行:
rainfall_condition[n] = "*";
应该是:
rainfall_condition[n] = '*';
在编译器中打开一些警告;第一行(现在的内容)不是有效的C代码,因此您应该会看到一条诊断消息。
编辑:既然我已经阅读了更多代码,看来您想要在该列中使用*
还是empty
?在这种情况下,您要将变量声明更改为:
char *rainfall_condition[50]; // array that contains the *
然后将打印语句更改为:
printf("%d \t %f \t %s \n", n + 1, rainfall_array[n], rainfall_condition[n]);