У меня есть несколько ошибок. Мне нужна помощь в понимании.Функции и указатели
Во-первых, вот тестовый прогон моей программы на 7 дней. Я хочу найти способ для функции void getDailyData(float* high, float* low, char* condition);
, чтобы остановить чтение символов после выполнения условия, если есть больше символов, которые я хотел бы запросить и сообщить об ошибке.
Во-вторых, моя программа не соблюдает условия в void getDailyData(float* high, float* low, char* condition);
функции, потому что здесь, как приходят пользователю разрешено поставить в этот вход
Enter today's high, low, and condition (c=cloudy, s=sunny, p=precipitation)
separated by commas:3.4 and not get flagged for it i don't understand how that could happen?
Enter an number between 1 and 14: 7
Enter today's high, low, and condition (c=cloudy, s=sunny, p=precipitation)
separated by commas:15.3, 10.8, s
Today's average temperature is: 13.05
@☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺Enter today's high, low, and condition (c=cloudy, s=sunny,
p=precipitation)
separated by commas:11.5, 5.0, c
Today's average temperature is: 8.25
~☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺Enter today's high, low, and condition (c=cloudy, s=sunny,
p=precipitation)
separated by commas:9.9, 3.3, p
Today's average temperature is: 6.60
;☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺Enter today's high, low, and condition (c=cloudy, s=sunny,
p=precipitation)
separated by commas:5.6, -0.8, p
Today's average temperature is: 2.40
;☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺Enter today's high, low, and condition (c=cloudy, s=sunny,
p=precipitation)
separated by commas:3.4
Today's average temperature is: 1.30
;☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺Enter today's high, low, and condition (c=cloudy, s=sunny,
p=precipitation)
separated by commas:3.4, -6.5, giraffe, elephant
ERROR! Try again!nEnter today's high, low, and condition (c=cloudy, s=sunny, p=p
recipitation)
separated by commas:-6.5, 3.4, p
ERROR! Try again!
Enter today's high, low, and condition (c=cloudy, s=sunny, p=precipitation)
separated by commas:3.4, -6.5, p
Today's average temperature is: -1.55
*☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺Enter today's high, low, and condition (c=cloudy, s=sunny,
p=precipitation)
separated by commas:-1.1, -11.0. p
Today's average temperature is: -6.05
*☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺☺
Your Seventh day average is: 3.43
Press any key to continue . . .
Код:
#include <stdlib.h>
#include <stdio.h>
void clearscr(void);
char symbolToDraw(char condition, float averageTemperature);
int getInteger(int min, int max);
float average(float first, float second);
void getDailyData(float* high, float* low, char * condition);
void displayResult(int days, float allDaysAverage);
void draw(char c, int num);
int main(void){
int numDays;
int i;
float sum = 0;
float dailyHigh;
float dailyLow;
char conditions;
float dailyAvg = 0;
//title
printf("Weather Analyzer 2.0\n");
printf("====================\n");
//prompt the user for a number of days
printf("How many days of data?\n");
//get an integer between 1 and 14
numDays = getInteger(1,14);
for(i = 0; i < numDays; i++){
//get input from the user
getDailyData(&dailyHigh, &dailyLow, &conditions);
dailyAvg = average(dailyHigh, dailyLow);
printf("Today's average temperature is: %.2f\n", dailyAvg);
sum += dailyAvg;
draw(symbolToDraw(conditions, dailyAvg), 20);
}
//remember, arguments are separated by commas
//there are two arguments here...
displayResult(numDays, (sum/numDays));
return 0;
}
/*
This function prompts the user to enter an integer between the min and the max
If the input entered by the user is invalid, it displays an error and asks again.
It will continue asking the user until the integer the user enters falls between
min and max (inclusive).
This function returns an integer, ensured to be between min and max (inclusive)
and assumes that min <= max
*/
int getInteger(int min, int max){
//your implementation here
int value, keeptrying = 1, rc;
char after;
do {
printf("Enter an number between 1 and 14: ");
rc = scanf("%d%c", &value, &after);
if (rc == 0) {
printf("*Bad char(s)!*\n");
clearscr();
} else if (after != '\n') {
printf("*Trail char(s)!*\n");
clearscr();
} else if (value < min ||
value > max) {
printf("*Out of range!*\n");
} else
keeptrying = 0;
} while (keeptrying == 1);
return value;
}
/*
This function prompts the user to enter a day's worth of data (high temperature,
low temperature, weather condition) separated by commas, validates the input such
that the user is forced to enter a floating point number for the high, a floating
point number for the low. It ensures that the low temperature is not higher than
the high temperature and that the condition is one of: 'c', 's', or 'p'.
If the user enters invalid input, this function displays an error and asks again.
float * high: a pointer holding the address of the float to which the function
will copy the day's high temperature before returning.
float * low: a pointer holding the address of a float to which the function will
copy the day's low temperature before returning.
char* condition: a pointer holding the address of the char to which the function
will copy the day's condition before returning.
*/
void getDailyData(float* high, float* low, char* condition){
int counter =0;
do{
printf("Enter today's high, low, and condition (c=cloudy, s=sunny, p=precipitation) \n separated by commas:");
scanf("%f, %f, %c", high, low, condition);
if(*low > *high){
printf("ERROR! Try again!\n");
clearscr();
}else if(!(*condition == 's' || *condition == 'p' || *condition == 'c')){
printf("ERROR! Try again!\n");
clearscr();
}
else counter =1;
}while(counter == 0);
}
/*
This function draws a row of characters (such as @@@@@@@).
char c holds the character that will be repeated
int num holds the number of times that the character will be repeated
The number of characters is controlled by the value of "num".
*/
void draw(char c, int num){
//your implementation here
int i;
for(i=0; i < num; i++){
printf("%c", c);
}
}
/*
This function returns the average of the first and second floating point number.
*/
float average(float first, float second){
//your implementation here
float avg;
avg = (first + second)/2;
return avg;
}
/*
This function displays the average temperature for a period of days
(e.g. Your seven day average is: 14.3)
NOTE: it displays the int days as a word; for example, 3 as "three"
If the int days is greater than 9 (nine) it just prints the value;
for example Your 10 day average is: 12.2)
*/
void displayResult(int days, float allDaysAverage){
//your implementation here
if(days == 1){
printf("\nYour One ");
}
if(days == 2){
printf("\nYour Second ");
}
if(days == 3){
printf("\nYour Third ");
}
if(days == 4){
printf("\nYour Forth ");
}
if(days == 5){
printf("\nYour Fifth ");
}
if(days == 6){
printf("\nYour Sixth ");
}
if(days == 7){
printf("\nYour Seventh ");
}
if(days == 8){
printf("\nYour Eigth ");
}
if(days == 9){
printf("\nYour Ninth ");
}
if(days >= 10){
printf("\nYour %d", days);
}
printf("day average is: %.2f\n", allDaysAverage);
}
/*
This function returns the correct character (@, ~, *, ;) given the current
condition and the average temperature.
NOTE: Precipitation character (* or ;) will change depending on the average
temperature.
char condition represents the current conditions
float averageTemperature represents the average daily temperature
*/
char symbolToDraw(char condition, float averageTemperature){
//your implementation here
char c = '~';
char p = ';';
char s = '@';
char l = '*';
if(averageTemperature < 0){
p = '*';
}
switch(condition){
case 's':
printf("%c", s);
break;
case 'p':
printf("%c", p);
break;
case 'c':
printf("%c", c);
break;
default:
break;
}
}
void clearscr(void){
while (getchar() != '\n'); // empty statement intentional
}
«Моя программа не соблюдает условия ... как пользователь может ввести этот вход». Потому что вы не проверяете возвращаемое значение 'scanf'. – kaylum