Printing out previously entered user input!

This is a discussion on Printing out previously entered user input! within the C Programming forums, part of the General Programming Boards category; I am creating a program that will continually take in the user inputs and when they press 'q', it will ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Location
    Canada
    Posts
    22

    Printing out previously entered user input!

    I am creating a program that will continually take in the user inputs and when they press 'q', it will show all the entered user inputs. Assuming that all the inputs are to be integers. What would be the syntax for this? I have the code but my specific problem is with displaying all the previously entered user inputs.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,734
    You probably would want to save each input entered into an array.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2011
    Location
    Canada
    Posts
    22
    Yes, I have two different arrays. One for negative and one for positive numbers but if I want to display all the positive numbers the user entered, what would be the syntax. All my arrays are of [10] size.

  4. #4
    C Newbie
    Join Date
    Oct 2011
    Posts
    59
    Quote Originally Posted by SimarGill View Post
    Yes, I have two different arrays. One for negative and one for positive numbers but if I want to display all the positive numbers the user entered, what would be the syntax. All my arrays are of [10] size.
    If you have the positive and negative numbers in separate arrays, then wouldn't you just use a for-loop and display all of the integers stored in it? (It might be helpful if you posted your code so I am not just speculating on what you actually need, or if you went into more detail.)

  5. #5
    Registered User
    Join Date
    Nov 2011
    Location
    Canada
    Posts
    22
    Yes, you are right and that is exactly what I can't figure out how to actually code the for loop. I am trying to use a for loop right now but can't seem to understand what to actually code it.

    Code:
               
                  for ( int i = 0; i < 10; i++ )
                  {
                  positiveArray[i] = user2; // info for user2 is calculated before the for loop and can only be a integer
                  printf ("Your numbers are: %d", positveArray); // Don't know how to syntax it properly
                  }
    Basically, I want to display all the numbers stored in the array

  6. #6
    C Newbie
    Join Date
    Oct 2011
    Posts
    59
    Quote Originally Posted by SimarGill View Post
    Yes, you are right and that is exactly what I can't figure out how to actually code the for loop. I am trying to use a for loop right now but can't seem to understand what to actually code it.

    Code:
               
                  for ( int i = 0; i < 10; i++ )
                  {
                  positiveArray[i] = user2; // info for user2 is calculated before the for loop and can only be a integer
                  printf ("Your numbers are: %d", positveArray); // Don't know how to syntax it properly
                  }
    Basically, I want to display all the numbers stored in the array
    First off,I would declare i at the beginning of the function instead of the in the for statement, so you would only be left with "i = 0" in the for statement instead of "int i = 0".

    Then, what exactly is user2 for? Might not be of any use for me to know but out of context it is weird lol.

    Then lastly, to fix what you are trying to print, you need
    Code:
    printf ("Your numbers are: %d", positveArray[i]);
    otherwise it won't print properly because you are telling it to print an entire array instead of a specific value in the array. (I think that's how it should be explained, I'm still a novice myself.)

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,734
    Quote Originally Posted by SimarGill
    Yes, I have two different arrays. One for negative and one for positive numbers but if I want to display all the positive numbers the user entered, what would be the syntax. All my arrays are of [10] size.
    It may be more flexible to just store the user input as-is into a single array. To display all the positive numbers entered, you loop over this array and print the current number if it is positive.

    Quote Originally Posted by Alan Gott
    First off,I would declare i at the beginning of the function instead of the in the for statement, so you would only be left with "i = 0" in the for statement instead of "int i = 0".
    I would keep it as written, unless I want the code to conform to the previous version of the C standard. Keeping the scope of i to the for loop follows the rule of thumb to declare variables near first use.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    C Newbie
    Join Date
    Oct 2011
    Posts
    59
    Quote Originally Posted by laserlight View Post
    I would keep it as written, unless I want the code to conform to the previous version of the C standard. Keeping the scope of i to the for loop follows the rule of thumb to declare variables near first use.
    Well, I have to use MSVC++ for my lab, so I have to keep in standards with C-89 since MSV doesn't like the new standards. As I said, I am a novice still and I was just trying to help in whatever way I could.

  9. #9
    Registered User
    Join Date
    Nov 2011
    Location
    Canada
    Posts
    22
    When my user enters 'E' or 'e' then it asks them for their input as an integer but now the problem is which index in the array should I save it to knowing that my program will continually run.

    Code:
    int main(void)
    {
    int array[10];
        while ( TRUE )
    {
        if ( user1 == 'E' || user1 == 'e' )
        {
             printf ("\nPlease enter an integer number: ");
             scanf ("%d", &user2);
             if ( user2 >= 0 )
             {
                  array[] = user2;
                  counterPositive++;
             }
             else if ( user2 < 0 )
             {
                  array[] = user2;
                  counterNegative++;
             }
             }
             totalCounter = counterPositive + counterNegative;
        }
    }
    }

  10. #10
    C Newbie
    Join Date
    Oct 2011
    Posts
    59
    Quote Originally Posted by SimarGill View Post
    When my user enters 'E' or 'e' then it asks them for their input as an integer but now the problem is which index in the array should I save it to knowing that my program will continually run.

    Code:
    int main(void)
    {
    int array[10];
        while ( TRUE )
    {
        if ( user1 == 'E' || user1 == 'e' )
        {
             printf ("\nPlease enter an integer number: ");
             scanf ("%d", &user2);
             if ( user2 >= 0 )
             {
                  array[] = user2;
                  counterPositive++;
             }
             else if ( user2 < 0 )
             {
                  array[] = user2;
                  counterNegative++;
             }
             }
             totalCounter = counterPositive + counterNegative;
        }
    }
    }
    If what you are asking is for a way for the loop to end, how about changing it to a do-while loop, or something, where it asks the user if he/she wants to repeat it.

    EX:
    Code:
    do{
    //code here
    printf("Do you want to go again? (1 for yes, 2 for no) ");
    scanf("%d", &choice);
    if(choice == 2)
         return 0;
    }while(choice == 1);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structure, user input and printing out results
    By alvarito in forum C Programming
    Replies: 3
    Last Post: 10-04-2011, 11:04 AM
  2. turns the name entered by the user
    By Magic-Man-fci in forum C++ Programming
    Replies: 1
    Last Post: 04-12-2011, 12:14 AM
  3. Printing a Solid Square based on user input
    By ThatBoy1124 in forum C Programming
    Replies: 3
    Last Post: 03-01-2011, 09:07 PM
  4. Printing asterisks using user input
    By celticpride in forum C Programming
    Replies: 4
    Last Post: 02-10-2011, 08:43 PM
  5. Replies: 2
    Last Post: 11-05-2010, 02:36 PM

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