1

When I added the main while loop, the program just always runs, and runs through again and with a message telling me there is invalid data (from my first input validation). Can anyone show me how to give the user the option to end the program? Basically, I just want to give the user the option to end the program by pressing 'y or 'n'. Would be open to hearing new ideas, such as using a for, or do while loop! I just want the program to go through. I want the user to be able to go through as many times as they want, while having the option to leave after each run through.

#include <stdio.h>

//Declare functions
void displayWelcome(void);
double getAverage(double yards, double carries);
void endMessage(void);


int main()
{
//Declare variables
    double yards, carries, average, bestAverage;
    char again = 'y';
//Call function to display welcome message
    displayWelcome();

    while (again == 'y' || again =='Y') {
//Prompt user for the number of yards,
    printf("Enter the number of yards:");
/*
Scan in the value of 'yards' and send user into a loop if they enter 
incorrect data
While loop to validate the data of the value 'yards'
*Works with a numerical value greater than 0
*/
    while ( 1 != scanf( "%lf", &yards ) || yards <= 0)
{

    fflush(stdin);
    printf("Enter a numerical value greater than zero:");
}
//Prompt user for the number of carries, followed by an input scan to get 
//value of carries
    printf("Enter the number of carries:");
/*
Scan in the value of 'yards' and send user into a loop if they enter 
incorrect data
    While loop to validate the data of the value 'carries'
*Works with a numerical value greater than zero
*/
    while ( 1 != scanf("%lf", & carries) || carries <= 0)
{
//Refreshes the input value of the variable 'carries' and prompts the 
//user to type in new value for 'carries'
    fflush(stdin);
    printf("Enter a numerical value greater than zero:");
}

    average = getAverage( yards, carries);
    printf("yards:%g carries:%g average:%g\n", yards, carries, average);
//Give the user the option to run the program again
   printf("Would you like to run the program again? Enter (y)es or (n)o\n");
   scanf("%c\n", &again);
}
   endMessage();
   return 0;
}

    void displayWelcome(void)
{
    printf("Welcome to Football Stats\n");
}

    double getAverage(double yards, double carries)
{
    double average;
    average = yards + carries / 2;
    return average;
}
    void endMessage(void){
    printf("\nThese results were brought to you by ");
}

UPDATE (QUESTION STILL UNANSWERED) It worked when I presses 'n' to stop the program. However, when I press 'y' to run it again (with the newly input code) it ALSO stops the program. So back to my question, can anyone see WHAT I am doing wrong so that I can give the user the option to close the program?

FINAL UPDATE I found the issue. I did "scanf("%c\n", &again);" (clearing the values) when I should have put "scanf("\n%c", &again);". Thank you to everyone who spent the time to help me!

Mohtsu
  • 31
  • 6
  • 2
    Your `while` condition is wrong. Change `(again == 'y' || 'Y')` to `(again == 'y' || again == 'Y')`. – John Bollinger Oct 03 '17 at 21:32
  • `scanf("%c\n", &again);`-- it is almost always wrong to use trailing whitespace characters in `scanf()` format strings; this tends to cause problems with interactive input. Most `scanf()` conversion specifiers automatically skip over leading whitespace characters, but `%c`, `%[]` and `%n` are exceptions. You can simply add a leading space to tell `scanf()` to skip leading whitespace characters (of which `\n` is one): `scanf(" %c", &again);`. Also, `fflush(stdin)` causes undefined behavior, according to the Standard; there are better ways to clear the input stream. – ad absurdum Oct 03 '17 at 23:23
  • Thank you for educating me on that Mr. Bowling , I love learning new things! Would you give me a few examples of how you would clear the input stream? I am still new-ish to C and would love any extra info you have to spare :) – Mohtsu Oct 04 '17 at 06:06
  • [Here](https://stackoverflow.com/questions/40958298/is-this-the-proper-way-to-flush-the-c-input-stream), are a [few links](https://stackoverflow.com/questions/40554617/while-getchar-n/40555134#40555134) pertaining to [clearing the input stream](https://stackoverflow.com/questions/36715002/how-to-clear-stdin-before-getting-new-input). And here is a link [about trailing whitespace in format strings](https://stackoverflow.com/questions/43032984/using-scanfd-with-a-space-after-the-d/43033218#43033218). – ad absurdum Oct 04 '17 at 07:06

3 Answers3

1
while (again == 'y' || 'Y') { ...

|| doesn't work like that. && and || are used to connect complete boolean expressions. What you mean is:

while (again == 'y' || again == 'Y') { ...

In C, the expression 'Y' is just the integer 89, which C interprets as "true" because it is not zero. Thus, your first expression was

while (<something> || <true>) { ...

which is always true.

Lee Daniel Crocker
  • 12,927
  • 3
  • 29
  • 55
  • Thank you for explaining the why behind this! Helped me learn something new, I appreciate it :) – Mohtsu Oct 03 '17 at 21:36
  • The original question was still unanswered, could you take a look and see if you know why? – Mohtsu Oct 03 '17 at 22:09
1

The conditions of your while loop evaluate to true. Like others have mentioned, (again == 'y' || 'Y') should be ((again == 'y') || (again == 'Y')). What I have not seen otherwise mentioned is that you also initialize again to 'n', which would then evaluate to false on the first run. Instead of initializing again, you could instead transform it do a do-while loop so you always go into the loop on the first time and then evaluate the condition.

do {
    ...
} while((again == 'y') || (again == 'Y'));
Christian Gibbons
  • 4,272
  • 1
  • 16
  • 29
0

Try:

while (again == 'y' || again == 'Y')

Instead of:

while (again == 'y' || 'Y')

Otherwise, your program thinks you only want to evaluate 'Y', which is always true (i.e. non-zero).

frslm
  • 2,969
  • 3
  • 13
  • 26
  • I still am unable to give the user the option to re-run the while loop. Instead, pressing y or n terminates the program. Are you able to see why this is? – Mohtsu Oct 03 '17 at 22:10