Question:

C Language Question?

by  |  earlier

0 LIKES UnLike

I'm writing the second program for my c language class and I'm hung up trying to get my simple program to loop. I am attempting to use a while loop to ask the user if they would like to use the program again. Here is my main function:

/* beginning of function main */

int main (void)

{

/* variable prototypes for function main */

int grade = 0;

char cgoAgain = 'y';

/* beginning of while loop for cgoAgain */

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

{

/* calls function getGrade to acquire user input */

grade = getGrade();

/* sends acquired input to function calculateGrade */

calculateGrade(grade);

printf("\nWould you like to calculate another grade (y/n)?");

scanf("%c%*c",&cgoAgain);

} /* end of while cgoAgain loop */

return 0;

}

in my scanf string I have to use %c%*c to hold my variable for cgoAgain but it never happens. The program works if I use %s instead but my instructor said we haven't gone over that yet and I can't use it. He also said there is an obvious error I am overlooking but won't give me any specific information.

Please Help!

 Tags:

   Report

5 ANSWERS


  1. I pulled your code and tried it.  Obviously I don't have the functions, so I left them out and just had the while loop with the printf and the scanf statements.  Works fine.  Put in a printf statement and the variable is set fine.  

    So I'm guessing that the error he's referring to is elsewhere, possibly in code you've removed for this post.  Don't know what error you're getting and without the rest of the code I can't guess.


  2. Man, it's obvious :

    scanf("%c%*c",&cgoAgain);

    Why u r using two data type specifiers  while ur asking for  ONLY one input  from the user  ?

    Should be

    scanf("%c",&cgoAgain);


  3. Open Question

    Show me another »

    C Language Question?

    I'm writing the second program for my c language class and I'm hung up trying to get my simple program to loop. I am attempting to use a while loop to ask the user if they would like to use the program again. Here is my main function:

    /* beginning of function main */

    # include std.h

    int main (void)

    {

    /* variable prototypes for function main */

    int grade = 0;

    char cgoAgain = 'y';

    /* beginning of while loop for cgoAgain */

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

    {

    /* calls function getGrade to acquire user input */

    grade = getGrade();

    /* sends acquired input to function calculateGrade */

    // your problem here is that you have no function for this call, nor

    // any math to calculate the grade itself

    calculateGrade(grade);

    printf("\nWould you like to calculate another grade (y/n)?");

    scanf("%c%*c",&cgoAgain);

    } /* end of while cgoAgain loop */

    return 0;

    }

    in my scanf string I have to use %c%*c to hold my variable for cgoAgain but it never happens. The program works if I use %s instead but my instructor said we haven't gone over that yet and I can't use it. He also said there is an obvious error I am overlooking but won't give me any specific information.

    Please Help!

    The problem is that you have to have a call for in put to calculate the grade itself, after you ask if one wants to calculate a grade.

    And you have no functions to calculate the grade itself only the function calls

    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    // to cal grade function

    Int calgrade(int a, int b)

    {

    return(a+b)

    }

    the call would look something like this

    int answer = calgrade(a,b);

    then a printf(“answer”);

    int main()

    {

        while { again == ‘y’ || again == ‘Y’)

    if (again == ‘y’ || again == ‘Y’)

    then

    {

    answer = calgrade(a,b);

    printf(answer);

    }

    else

    printf(“bye”);

    }//end while

    printf(“would you like to cal grade (y/n)?);

    scanf( what ever code comes in to get char)

    then you need to printf again to get numbers to add to cal the grade)

    scanf (Int, int, int until you have enough to add, in this case you should only be doing two integers)

    then it should loop back into your while statement and calculate it.


  4. did your professor teach you about includes.

    #include <stdio.h> at the beginning of the file. and any others that you need as well.

  5. Are you sure is %c%*c and not just %c??

    This is what I would do:

    printf("\nWould you like to calculate another grade (y/n)?");

    scanf("%c",&cgoAgain);

    and %s is used for arrays, trust me you don't need to worry with that just yet.
You're reading: C Language Question?

Question Stats

Latest activity: earlier.
This question has 5 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.