Thread: Beginner's Problem

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    23

    Beginner's Problem

    I'm a novice C programmer using the Ubuntu terminal to compile. As in, I just started today. The compiler keeps coming up with this:

    Variable1.c: In function 'main':
    Variable1.c:9:46: error: expected ')' before string constant

    I checked all over for mismatched parentheses. Here is the code.
    Code:
    #include <stdio.h>
    
    int main()
    {
      int this_is_a_number;
    
      printf ( "Please enter a number: " );
      scanf ( "%d", &this_is_a_number );
      printf ( "You entered %d", this_is_a_number"\n" );
      getchar();
      if ( this_is_a_number==1 ){
        printf ( "Correct! \n");
      }
      else {
        printf ( "Incorrect.  Press enter to be exited from the program. \n");
      }
      getchar();
      return 0;
    }
    Any ideas on what is wrong? I am a beginner, so it's probably something fundamental that I missed.

  2. #2
    Registered User
    Join Date
    Nov 2011
    Posts
    23
    By the way, this is just experimental. There is absolutely no purpose to this program than a learning experience.

  3. #3
    Registered User joybanerjee39's Avatar
    Join Date
    Oct 2011
    Location
    kolkata
    Posts
    106
    Quote Originally Posted by Gigabitten View Post
    I'm a novice C programmer using the Ubuntu terminal to compile. As in, I just started today. The compiler keeps coming up with this:

    Variable1.c: In function 'main':
    Variable1.c:9:46: error: expected ')' before string constant

    I checked all over for mismatched parentheses. Here is the code.
    Code:
    #include <stdio.h>
    
    int main()
    {
      int this_is_a_number;
    
      printf ( "Please enter a number: " );
      scanf ( "%d", &this_is_a_number );
      printf ( "You entered %d", this_is_a_number"\n" );
      getchar();
      if ( this_is_a_number==1 ){
        printf ( "Correct! \n");
      }
      else {
        printf ( "Incorrect.  Press enter to be exited from the program. \n");
      }
      getchar();
      return 0;
    }
    Any ideas on what is wrong? I am a beginner, so it's probably something fundamental that I missed.
    the problem is in this statement->
    printf ( "You entered %d", this_is_a_number"\n" );
    Last edited by joybanerjee39; 11-25-2011 at 11:47 PM.

  4. #4
    Team Bring It rajarshi's Avatar
    Join Date
    Nov 2011
    Location
    India
    Posts
    79
    Quote Originally Posted by Gigabitten View Post
    By the way, this is just experimental. There is absolutely no purpose to this program than a learning experience.
    replace line 9 with
    Code:
    printf( "You entered %d \n", this_is_a_number);

    " I failed in some subjects in exam , but my friend passed in all . Now he is an engineer in Microsoft and I am the owner of Microsoft !! "

    - Bill Gates .

  5. #5
    Registered User
    Join Date
    Nov 2011
    Location
    Buea, Cameroon
    Posts
    197

    character and integer problem

    hey giga i am a beginner too just been into c for about a month now i think the problem with the code is the fact that you declared and integer and used 'getchar' which is for characters to collect an integer.
    i have not ran the code yet i may be try scanf and see what happens

  6. #6
    Registered User
    Join Date
    Nov 2011
    Posts
    16
    The problem was what ragarshi stated....include the '\n' in the quotation marks after the %d and it'd work.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Nyah Check View Post
    hey giga i am a beginner too just been into c for about a month now i think the problem with the code is the fact that you declared and integer and used 'getchar' which is for characters to collect an integer.
    i have not ran the code yet i may be try scanf and see what happens
    You need to go and look up the return value from getchar() ....

  8. #8
    Registered User
    Join Date
    Nov 2011
    Posts
    23
    Such a simple syntax error...thanks, guys! I was amazed at how fast the answer came!

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Understand what the error means though. The "\n" is a string constant, and the compiler can only work with 1 variable in that part of the printf() code. There is no format in the format section, for another variable of any kind - it expects the closing parenthesis: ) (and semi-colon)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner problem help
    By Unfawkable in forum C Programming
    Replies: 4
    Last Post: 05-12-2011, 01:45 PM
  2. New Problem [Beginner]
    By Vintik in forum C++ Programming
    Replies: 5
    Last Post: 08-16-2005, 11:23 PM
  3. A beginner's problem
    By NewToC in forum C Programming
    Replies: 5
    Last Post: 11-21-2002, 05:20 PM
  4. beginner problem
    By laasunde in forum C Programming
    Replies: 0
    Last Post: 11-21-2002, 08:24 AM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 05:46 PM

Tags for this Thread