Thread: Collect multiple lines of inputs

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    35

    Collect multiple lines of inputs

    Hello:
    I am trying to do this assignment: http://icpcres.ecs.baylor.edu/online...m&problem=1464
    This is the code which I have completed already:
    Code:
    int main(int argc, char *argv[])
    {
         int n,a,i,sum=0;
         scanf("%d",&n);
         scanf("%d",&a);
         for(i=1; i<=n; i++)
                    {
                     int calc,x,final_a = a;
                     for(x=1;x<i;x++)
                        {
                         final_a = final_a * a;
                        }
                     calc = i * final_a;
                     sum = sum + calc;
                    }
         printf("%d\n",sum);
         
    }
    However; as you can see on that website; there needs to be two lines of input and the 2 lines of output. How do I loop it so I can collect more then 1 line of input? The assignment does not say how many lines of input nor does it ask to collect it from the user. So I do not understand. Right now my application collects two inputs and then prints out 1 line of output and breaks. How else should I do this?

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    char buffer[100]
    while(fgets(buffer, sizeof buffer, stdin))
    {
       /* parse buffer to get two integers */
    
       /* and call the function to perform calculations */
    
       /* output result */
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    35
    Thanks for your reply;
    but what does that code exactly do? The thing is I do not know if it will be EXACTLY 2 lines of input or not. If I knew it would be exactly a number of inputs; then it would be so much easier. I could just do a for loop. But I don't know what to do right now. It seems like there is an input file.

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    It is an input file that will be directed as input stream
    so you just read from stdin till EOF (as in a sample code I provided)

    your program will be run as
    test.exe <input.txt >output.txt
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    35
    Thanks for the clearification.
    I have not learned about buffers yet. What topic would they be under? (So i can read about them in my textbook.)
    In your sample code; you have a comment saying parse buffer to get two integers; how would I do that?

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    290
    You could do this:
    Code:
    total = -1;
    
    while (scanf(" %d %d ", &n, &a) == 2) {
       if (total > -1) putchar('\n');
    
       // do your calculations and add to total
    
       // print total (without a newline)
    }
    That will print EXACTLY the number of lines corresponding to the number of inputs. It of course relies on the fact that, based on the input specification, a total of -1 could never happen. If it could, then you'd have to use a different value, or resort to a boolean flag.

    Problems in programming competitions often require you to conform to tricky input/output specifications like this, and a trailing newline could get you disqualified.

  7. #7
    Registered User
    Join Date
    Feb 2008
    Posts
    35
    So how does the code tell the input has ended? is this based on scanf() inputs or file based inputs.
    The exact words the problem says is: "For each line of the input, your correct program should output the integer value of the sum in separate lines for each pair of values of N & A."
    Like right now this is the code I have after your last post:
    Code:
    int main(int argc, char *argv[])
    {
         int total,n,a,i,sum=0;
         while (scanf(" &#37;d %d ", &n, &a) == 2) {
         if (total > -1) putchar('\n');
         for(i=1; i<=n; i++)
                    {
                     int calc,x,final_a = a;
                     for(x=1;x<i;x++)
                        {
                         final_a = final_a * a;
                        }
                     calc = i * final_a;
                     sum = sum + calc;
                    }
         total++;
         printf("%d",sum);
         }
         system("PAUSE");
         return 0;
    }
    I enter for example
    3 3
    4 4
    the output is:
    102
    nothing else.
    I still dont really understand what the problem means by that sentence. Do you think it wants me to figure out how many inputs they are going to give the app or is it ONLY 2 and ALWAYS 2?
    Thanks!

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    And since the output is nothing else ... that means you're also not getting the system("PAUSE") output ... because you're still in the loop. You're program runs until EOF or until an input failure; that means you must type something that is not a number.

    All ACM programs expect input to be in a file, so it would read lines until EOF.

  9. #9
    Registered User
    Join Date
    Feb 2008
    Posts
    35
    Hmm... now I am really confused. Because I am maybe thinking about this problem way harder then it is. I am thinking about using pointer, dynamic arrays, and such...
    Basically I understand I can keep getting input until EOF. But then; how do I store all of these different input? Since each pair of input outputs a single int; I would need to store each integer separately and print them out at a later time. Do I need to use the dynamic array system here to create an array that I do not know the size of "yet"? Then I can just add each integer to the array with every pass of the input loop...? At the end; I will loop through the entire size of the integers array and print each integer out on a new line.
    This is the only way I can think of how to do this problem.
    I think the code I posted in my last post is much too simple to achieve what the problem is asking for.
    I know that nobody should just give me the answer; but I am stuck here and I just need to see some types of code-examples; more then just a template. Make the example a different application so I can learn from it and solve this problem myself...
    Thanks!

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Yes, you are way way way way way way way way way way way over-complicating the issue; the point I think you have missed is that you only ever need one line at a time; once the answer has been calculated and output, you don't need that answer any more.

    So, using the EOF idea:

    Code:
    fscanf(input_file, "%d %d", &num1, &num2);
    while (!feof(input_file)) {
        //calculate
        //more calculate
        //output answer using fprintf
        fscanf(input_file, "%d %d", &num1, &num2); //throw away old values and get new ones
    }

  11. #11
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    or no... your loop will be endlees if file contains somethinf else than two ints in a line...

    Code:
    while (fscanf(input_file, "&#37;d %d", &num1, &num2) == 2)
    {
        /* do stuff */
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Monitors Supporting Multiple Inputs
    By SlyMaelstrom in forum Tech Board
    Replies: 3
    Last Post: 08-22-2006, 04:15 PM
  2. Replies: 1
    Last Post: 06-07-2006, 09:42 AM
  3. A problem with reading multiple lines from a file
    By edd1986 in forum C++ Programming
    Replies: 6
    Last Post: 03-19-2006, 01:26 PM
  4. Multiple lines on an Edit box
    By RubenJ in forum Windows Programming
    Replies: 3
    Last Post: 09-20-2001, 02:51 PM
  5. Getting multiple inputs using cin
    By edk in forum C++ Programming
    Replies: 2
    Last Post: 09-13-2001, 02:34 PM