Thread: Quick H/W question...

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

    Post Quick H/W question...

    I am making a program that takes a 10 digit number, and using a process makes a verification #. But I would like to display the output on the same line as the input. Any help is appreciated, C code would be great but C++ is fine also.

    Example:
    10 Digit # 10 Digit # + Validation
    6458796218 6458796218Q

    Mine looks like:
    10 Digit # 10 Digit # + Validation
    6458796218
    6458796218Q

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    You need to show us some of your code. Not only would that help us give you a more specific solution to the problem, it also helps us see more clearly what your problem is. Me thinks you just need to fiddle with some of the formatting commands (I only know how to do this in C++, however, I'm sure this is still relatively easy in C).

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    The program only prints what you tell it to. Somewhere you're printing a newline either with '\n' or endl that you should remove for the formatting to be like you want it.
    My best code is written with the delete key.

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

    Here is my code if this helps...

    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("\r%8d%10d%10d%12d%2d%2d%2d%2d%c",total,vchar-65,pair1,pair2,pair3,pair4,pair5,vchar);	
    	
    
    
    
    	fflush(stdin);
    	getchar();
    }

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

    ignore the \r in the printf...

    Ignore the \r in the printf...I was tried \b and \r and neither helped.

  6. #6
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    void main is a no no.. Im sure Prelude will tell you all about that in just a second..

    And this is really a C question and will probably get more responces in the cprogramming board.
    What is C++?

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >#include <conio.h>
    Gah!

    >void main(void)
    Ack!

    >fflush(stdin);
    *Evil overload*
    *ANSI nazi mode engaged*

    Let's go over this line by line:

    >#include <conio.h>
    No, you aren't even using anything from conio.h, so don't include it.

    >void main(void)
    Let's put it this way, unless you're on a freestanding implementation, this is very wrong. If you don't know what a freestanding implementation is or aren't sure if you're on one, you aren't. main returns int.

    >scanf("%2d%2d%2d%2d%2d", &pair1, &pair2, &pair3, &pair4, &pair5);
    scanf will return the number of successful conversions that were made. If this number does not match the number of variables you passed to it, something untoward happened and you need to recover. But if you don't check, you won't know, and you'll probably invoke undefined behavior. Just so you know, undefined behavior is probably the worst thing that could happen in a C or C++ program.

    >fflush(stdin);
    fflush isn't defined for input streams. This is that undefined behavior thing I was talking about.

    Can you give an example of the input that is causing this problem?
    My best code is written with the delete key.

  8. #8
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Code:
    Example:
    10 Digit # 10 Digit # + Validation
    6458796218 6458796218Q
                   ^
                  This is what is diplayed afterwards.
    ^ This is what the user types
    His problem is when the user hits enter it goes to the next line. He doesnt want this.

    [note] Sorry, bad habbit of talking for people
    What is C++?

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

    Thanks Crap Cracker, =)

    the fflush thing is to pause the program while using Visual C++ to compile the program, I just took this out because I switched to command line mode instead. About the scanf I will add an if statement that checks the total # of things scaned. But It's a rough program right now, I just want to get it working correctly. Then I will put all the checks in place, this is only my 3rd week in class. If I can modify my code to display on the same line as the scanf then I will be happy!

  10. #10
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>Thanks Crap Cracker, =)
    LOL Nice.

    >>If I can modify my code to display on the same line as the scanf then I will be happy!
    Hmm. I've never heard of a way to do this, and I can't think of any time when this would have been terribly useful. Is it an absolute requirement for your assignment that the output goes on the same line as the input?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  11. #11
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    If you have two string variables (one for storing the user-input and one for the validated "number") could you not just clear the screen (system("cls")) and display both variables at the same time?

    Or, I'm not sure about the exact code for this, but I thought you could "print" a backspace using printf...would that not return you to the line above? Or would that just keep you at the beginning of whichever line you were on? I'm not too familiar with the backspace thing, I just happened to read about it in a thread a couple of days ago...I'll try and find it and post the link...

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >could you not just clear the screen (system("cls")) and display both variables at the same time?
    Yes, that would work.

    >would that not return you to the line above?
    No, it wouldn't. But that sure would be convenient.
    My best code is written with the delete key.

  13. #13
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    Found it. Frobozz talks about a different way to clear the screen (3 posts down):
    http://cboard.cprogramming.com/showt...ght=system+cls

    I think it will also reduce the delay that system("cls") causes.

    To "clear the screen", you could also just print "\n" (new lines) the number of times that the console has rows. (Which is also discussed in that thread above.) But then if you had many lines displayed on the screen, you'd be able to see them all shoot up off the top...so maybe that isn't as good of an idea.

    Anyways, there's the thread I promised.

    >No, it wouldn't. But that sure would be convenient.
    As would a "\-n"

  14. #14
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    The problem with that method would be that the cursor would then be placed on the bottom line of the console? Is there a C/C++ equivalent of BASIC's LOCATE statement?

  15. #15
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>Is there a C/C++ equivalent of BASIC's LOCATE statement?
    You mean gotoxy()? I don't believe so (not a standard one anyway). There is the method mentioned in the FAQ which clears the screen quickly and places the cursor at the top left of the screen.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Very quick math question
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 10-26-2005, 11:05 PM
  2. very quick question.
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 07-24-2002, 03:48 AM
  3. quick question
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2002, 04:44 AM
  4. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM