Thread: Noob question with formating output

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    5

    Noob question with formating output

    I'm trying to learn C on my own and am having a problem with an example from C Plus Primer 4th edition chapter 4.

    Write a program that requests the user's first name and then the user's last name. Have it print the entered names on one line and the number of letters in each name on the following line. Align each letter count with the end of the corresponding name.
    I can do everything except for aligning the characters. I can get the strlen(firstname) but how can I put the number right under the last letter of the first name? If the first name is Bob then 3 should be under the b. I looked through the chapter again and there isn't anything that I could use to help me do that.

  2. #2
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    You need to put a newline and then make a loop
    In psuedocode -
    Code:
    PRINT NEWLINE;
    WHILE TEMP < LENGTH
         PRINT ' ';
    PRINT LENGTH;
    Last edited by MadCow257; 03-14-2005 at 08:55 PM.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    5
    Thanks, that's one way but the book hasn't even covered loops yet. They do in the next chapter though. Do you think there could be an easier way or should this question of been in the next chapter?

  4. #4
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    There is an easier way that doesn't require a loop, but it wouldn't be the answer he was looking for. It might be supposed to be in the next chapter, but I can't be sure. I don't even know c

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    5
    Quote Originally Posted by MadCow257
    There is an easier way that doesn't require a loop, but it wouldn't be the answer he was looking for. It might be supposed to be in the next chapter, but I can't be sure. I don't even know c
    Well you still know more than me... For now at least. Thanks for your insight though.

  6. #6
    Registered User
    Join Date
    Mar 2005
    Posts
    5
    Got another question and rather than create a clutter the main forum with noob questions I'll just post it here.. I can't figure out how to give the user 3 tries to enter the password with this program. I tried making nested do whiles and incrimenting a counter and that still doesn't work. It just ignores it and keeps on going until the password is entered. Here's the origional code. What's the best way for me to modify it?

    Code:
    /* dowhile.c -- exit condition loop */
    #include <stdio.h>
    int main(void)
    {
        const int secret_code = 13;
        int code_entered;
    
        do
        {
            printf("To enter the triskaidekaphobia therapy club,\n");
            printf("please enter the secret code number: ");
            scanf("%d", &code_entered);
        }  while (code_entered != secret_code);
        printf("Congratulations! You are cured!\n");
        getchar();
        getchar();
        return 0;
    }

  7. #7
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    Code:
    /* dowhile.c -- exit condition loop */
    #include <stdio.h>
    int main(void)
    {
        const int secret_code = 13;
        int code_entered, counter = 1;
        do
        {
            printf("To enter the triskaidekaphobia therapy club,\n");
            printf("please enter the secret code number: ");
            scanf("%d", &code_entered);
    	if (counter == 3)
    	{
    		printf("Failure");
    		getchar();
    		getchar();
    		return 0;
    	}
    	else
    		counter++;
        }  while (code_entered != secret_code);
        printf("Congratulations! You are cured!\n");
        getchar();
        getchar();
        return 0;
    }

  8. #8
    Registered User
    Join Date
    Nov 2004
    Location
    USA
    Posts
    516
    what you need is something of this sort
    Code:
     int count=0;
     int maincode=13;
     int code;
     do
     {
      printf("enter the password");
      scanf("%d",&code);
     }while(code != maincode && ++ count < 3);
     . . . . . . . . . 
     . . . . . . . . .
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

  9. #9
    Registered User
    Join Date
    Mar 2005
    Posts
    5
    Awsome, I went with a hybrid between the two suggestions. Both ways taught me something, thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Noob question (redeclaring a array)
    By Demon.killer in forum C Programming
    Replies: 8
    Last Post: 10-21-2006, 12:06 PM
  2. Noob Question
    By bumfluff in forum C++ Programming
    Replies: 4
    Last Post: 11-06-2005, 04:36 AM
  3. Noob here...a simple question.
    By The_Mayor in forum C Programming
    Replies: 4
    Last Post: 06-08-2005, 12:48 AM
  4. Formating output
    By eth0 in forum C++ Programming
    Replies: 4
    Last Post: 01-14-2004, 12:30 PM
  5. Simple question on quoting output
    By LouB in forum C++ Programming
    Replies: 13
    Last Post: 06-16-2002, 02:57 PM