Homework Question...

This is a discussion on Homework Question... within the C Programming forums, part of the General Programming Boards category; I'm trying to display a result on the same line as the user inputs the data. How do I do ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    8

    Homework Question...

    I'm trying to display a result on the same line as the user inputs the data. How do I do this? Everytime I try to do this, it displays on the next line.

    Example:

    I get this:
    Code:
    Input Code      10 Digit Sum    Numeric         Validated
                                                   Code Value   Code
    
    1234567890
         270            12          3456789075p
    I want this:
    Code:
    Input Code      10 Digit Sum    Numeric         Validated
                                                   Code Value    Code Value
    
    1234567890     270                12                  3456789075p
    here is my code:
    Code:
    //Lab assignment 1b-b
    //produces a validation letter for a 10 digit #
    
    #include <stdio.h>
    #include <conio.h>
    
    
    void main(void)
    {
    	int pair1, pair2, pair3, pair4, pair5, total, vchar;
    	
    	printf("Input Code	10 Digit Sum	Numeric		Validated\n");
    	printf("		    Code Value	    Code Value  Code\n\n");
    	
    	scanf("%2d%2d%2d%2d%2d", &pair1, &pair2, &pair3, &pair4, &pair5);
    	total=(pair1+pair2+pair3+pair4+pair5);
    	vchar=total%26+65;
    	printf("%8d%10d%10d%12d%2d%2d%2d%2d%c",total,vchar-65,pair1,pair2,pair3,pair4,pair5,vchar);	
    	
    
    
    
    	fflush(stdin);
    	getchar();
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Ooooooooh, I see what you mean now. Sorry, you can't portably fix it. Input in C is line based, which means you need to hit enter for any input to be sent to the program. Because of this that newline will be printed before you can do anything about it. Try this and see if you can tolerate it:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
      int pair1, pair2, pair3, pair4, pair5, total, vchar;
    
      scanf("%2d%2d%2d%2d%2d", &pair1, &pair2, &pair3, &pair4, &pair5);
    
      total=(pair1+pair2+pair3+pair4+pair5);
      vchar=total%26+65;
    
      printf("Input Code	10 Digit Sum	Numeric		Validated\n");
      printf("		    Code Value	    Code Value  Code\n\n");
      printf("%2d%2d%2d%2d%2d", pair1, pair2, pair3, pair4, pair5);
      printf("%8d%10d%10d%12d%2d%2d%2d%2d%c\n",total,vchar-65,pair1,pair2,pair3,pair4,pair5,vchar);	
    
      getchar();
    
      return 0;
    }
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    8

    it is supposed to be looped...

    Is there any way of doing that? How about in C++ if C can't handle it.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Is there any way of doing that?
    ...Yes, but I don't recommend it. And it pains me to know in my heart that you're going to rejoice when you see it work like you want and that you're going to use it. And it pains me even more to know that you'll continue to use it when you don't need to despite portable options:
    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int main(void)
    {
      int pair1, pair2, pair3, pair4, pair5, total, vchar;
    
      printf("Input Code	10 Digit Sum	Numeric		Validated\n");
      printf("		    Code Value	    Code Value  Code\n\n");
    
      cscanf("%2d%2d%2d%2d%2d", &pair1, &pair2, &pair3, &pair4, &pair5);
    
      total=(pair1+pair2+pair3+pair4+pair5);
      vchar=total%26+65;
    
      printf("%8d%10d%10d%12d%2d%2d%2d%2d%c\n",total,vchar-65,pair1,pair2,pair3,pair4,pair5,vchar);	
    
      getchar();
    
      return 0;
    }
    >How about in C++ if C can't handle it.
    C++ would suffer the same problem, and have the same non-portable solution.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    8

    Yes it woks!

    Why does it pain you? And what does more "portable options" mean? I'm new to programing but I want to be a good programmer.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Why does it pain you?
    I'm very anal about writing code that conforms to the C standard. Introducing a non-standard header and function for some frivolous formatting chafes.

    >And what does more "portable options" mean?
    It generally means compromising nifty features for the ability to compile your code on any machine with a C compiler and have it work properly.
    My best code is written with the delete key.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It is my sworn duity (because I swear a lot), to damn you for your post! And so I shall!
    Code:
    void main(void)
    Go read this FAQ entry now!
    Code:
    fflush(stdin);
    When you're finished, go read this FAQ entry!

    Heed my words, neophyte, lest you be haunted by the ghosts of coders past, and posters present! (Mainly posters present, like myself!)

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

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    > And what does more "portable options" mean?
    It also means you don't have to learn (and relearn and relearn and ....) a whole bunch of stuff each time you upgrade or change your compiler.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question About My Homework Pls Help Me İmmediately
    By jennyyyy in forum C Programming
    Replies: 27
    Last Post: 03-13-2008, 11:40 AM
  2. quick question
    By surfingbum18 in forum C Programming
    Replies: 5
    Last Post: 12-04-2007, 05:16 AM
  3. Design layer question
    By mdoland in forum C# Programming
    Replies: 0
    Last Post: 10-19-2007, 04:22 AM
  4. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 08:22 PM
  5. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 02:57 PM

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