Thread: Is there any way to have more than one input on a single line?

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    51

    Is there any way to have more than one input on a single line?

    I don't mean my line of code. In the program I enter one number and then it goes to a new line. I want a way around this if possible. (Very new to C)

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You mean to type more than one thing and then hit enter?

    1 2 3<enter>

    Sure, scanf will let you set up formatted input. Your book should be covering that soon.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    It's probably not impossible but if you show us your code it will be easier to say how.

    If by "it goes to a new line" you mean because the user had to hit enter, then you will probably need to use some special terminal library for this; also let us know what OS you are using.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Jul 2011
    Posts
    51
    quzah, it is for an assignment for a class I'm taking. The assignment does not specify if this is needed, I just prefer it. So, if you could, please elaborate?

  5. #5
    Registered User
    Join Date
    Jul 2011
    Posts
    51
    MK27, here's my code:
    Code:
    #include <stdio.h>
    #include "genlib.h"
    #include "simpio.h"
    
    main()
    {
          int num1, num2, num3, num4, sum, average;
          
          printf ("Your grades were: ");
          num1 = GetInteger();
          num2 = GetInteger();
          num3 = GetInteger();
          num4 = GetInteger();
          sum = num1 + num2 + num3 + num4;
          average = sum / 4;
          printf ("The average grade is %d", average);
          getchar();
          }
    My OS is Windows 7 Ultimate.
    Last edited by PYROMANIAC702; 07-01-2011 at 04:22 AM.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Hmm, so do you mean the problem quzah pointed out (that the user has to enter the grades one by one)?

    We also need to see the GetInteger() function, probably you want to use some combination of scanf() and sscanf() and return a pointer to an int array, or fill in an array submitted as an argument:

    Code:
    int *GetInteger();  // returns pointer to malloc'd array
    void GetInteger (int *data);  // fills in submitted array
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    Jul 2011
    Posts
    51
    You are speaking a whole different language to me man... But thanks for your help! I honestly appreciate it.

  8. #8
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Yeah the easy way to do this using scanf function. By the time your class cover scanf function. It would look something like this

    Code:
    scanf("%d %d %d %d", &num1, &num2, &num3, &num4);
    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  9. #9
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Also:
    Code:
    #include <stdio.h>
    #include "genlib.h"
    #include "simpio.h"
    
    int main(void)
    {
    ........
         return (0);
    }
    Note the declaration of main and the return value. The main function returns an int to the system, this is required to be C99 compliant. Some suggested reading:
    How to declare main()
    Get a number from the user.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Single comment line without C99?
    By dxfoo in forum C Programming
    Replies: 5
    Last Post: 07-13-2010, 06:24 PM
  2. Replies: 2
    Last Post: 02-16-2010, 02:04 PM
  3. Clearing single line?
    By louieansonng in forum C Programming
    Replies: 14
    Last Post: 07-26-2009, 11:10 PM
  4. Calcuate single line of 2d array
    By swgh in forum C++ Programming
    Replies: 7
    Last Post: 11-21-2007, 04:38 AM
  5. Single-line edit box?
    By BubbleMan in forum Windows Programming
    Replies: 1
    Last Post: 09-09-2001, 08:59 PM