Like Tree1Likes
  • 1 Post By Salem

C Program, Need help with finishing touches.

This is a discussion on C Program, Need help with finishing touches. within the C Programming forums, part of the General Programming Boards category; Im almost done writing this code, but i cant get it to stop on the 2nd or 4th question to ...

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    2

    C Program, Need help with finishing touches.

    Im almost done writing this code, but i cant get it to stop on the 2nd or 4th question to take an input. Any suggestions?

    Code:
    #include<stdio.h>
    int main()
    {
    int ch=1,i;
    while(ch==1)
    {
    printf("\n1: What is the correct value to return to the operating system upon the successful completion of a program\nA. -1 \nB. 1 \nC. 0 \nD. Programs do not return a value.");
    printf("\nEnter Your choice:");
    char ch1;
    scanf("%c",&ch1);
    if((ch1=='C')||(ch1=='c'))
    {
    i++;
    printf("\ncorrect answer");
    }
    else
    {
    printf("\nIn Correct Answer");
    }
    printf("\n2: What is the only function all C programs must contain\nA. start()\nB. system()\n C. main()\n D. program()\n");
    printf("\nEnter Your choice:");
    char ch2;
    scanf("%c",&ch2);
    if((ch2=='C')||(ch2=='c'))
    {
    printf("\ncorrect answer");
    i++;
    }
    else printf("\nIn Correct Answer");
    printf("\n3: What punctuation is used to signal the beginning and end of code blocks\nA. { } \nB. -> and <- \nC. BEGIN and END \nD. ( and )");
    printf("\nEnter Your choice:");
    char ch3;
    scanf("%c",&ch3);
    if((ch3=='A')||(ch3=='a'))
    {
    printf("\ncorrect answer");
    i++;
    }
    else printf("\nIn Correct Answer");
    printf("\n4: What punctuation ends most lines of C code\nA. . \nB. ; \nC. : \nD. ' ");
    printf("\nEnter Your choice:");
    char ch4;
    scanf("%c",&ch4);
    if((ch4=='B')||(ch4=='b'))
    {
    printf("\ncorrect answer");
    i++;
    }
    else printf("\nIn Correct Answer");
    printf("\n5: Which of the following is a correct comment?\nA. */ Comments */\nB. ** Comment **\nC. /* Comment */\nD. { Comment }");
    printf("\nEnter Your choice:");
    char ch5;
    scanf("%c",&ch5);
    if((ch5=='C')||(ch5=='c'))
    {
    printf("\ncorrect answer");
    i++;
    }
    else printf("\nIn Correct Answer");
    
    printf("score is ....%d",i);
    printf("Enter 1 to retake the quiz");
    scanf("%d",&ch);
    }
    return 0;
    }

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    %c on scanf will not eat the newline character like %d does.
    You will need
    Code:
    scanf(" %c",...);
    Also check your Indent_style
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,851
    For better readability of the code, you can wrap long lines like so.
    Code:
    printf("\n1: What is the correct value to return to the operating system "
           "upon the successful completion of a program\n"
           "A. -1 \nB. 1 \nC. 0 \n"
           "D. Programs do not return a value.");
    Also, by convention, newlines go at the end of a printf call to ensure that the stdout stream is flushed.

    If you need to print a prompt without a newline, then do this
    Code:
    printf("Prompt > ");
    fflush(stdout);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    language hopper dennis.cpp's Avatar
    Join Date
    Jan 2011
    Location
    Herborn, Hesse, Germany
    Posts
    197
    Quote Originally Posted by Salem View Post
    Also, by convention, newlines go at the end of a printf call to ensure that the stdout stream is flushed.
    @Salem - sorry for hijacking, but does C differ from C++ here? As I understood it, in C++, "endl" flushes the stream while '\n' does not. Does it in C? Or did I get the whole matter wrong?
    Hardware: Intel® Core™ i7-3630QM CPU @ 2.40GHz × 8
    Operating system: Debian Testing/Jessie / Linux 3.16-2-amd64 / KDE SC 4.14.1
    Compiler: gcc 4.9.1-16 with KDevelop 4.7.0

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,851
    This is what the C standard says
    Quote Originally Posted by c99
    When a stream is line buffered, characters are intended to be
    transmitted to or from the host environment as a block when a new-line character is
    encountered.
    Since stdout is line buffered by default, each \n should cause the internal buffer to be sent to the host environment.
    But if you decide to mess things up with setbuf() or setvbuf(), then you're on your own at that point.

    I believe you are correct when it comes to C++, in that it's only the endl manipulator which guarantees this behaviour.
    dennis.cpp likes this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Dec 2012
    Posts
    2
    Quote Originally Posted by std10093 View Post
    %c on scanf will not eat the newline character like %d does.
    You will need
    Code:
    scanf(" %c",...);
    Also check your Indent_style
    Thanks, that worked perfectly.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help finishing this program
    By j.ward91 in forum C Programming
    Replies: 16
    Last Post: 04-26-2012, 11:44 AM
  2. Finishing up Mastermind game program
    By Charak in forum C Programming
    Replies: 5
    Last Post: 02-17-2011, 02:49 AM
  3. Replies: 3
    Last Post: 12-03-2003, 10:15 AM
  4. SO close to finishing chat program...!
    By Nakeerb in forum C++ Programming
    Replies: 13
    Last Post: 10-26-2002, 01:24 PM
  5. finishing up the sorting program
    By jk in forum C Programming
    Replies: 2
    Last Post: 03-19-2002, 07:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21