Thread: Another Homework Question

  1. #1
    Registered User Trekkie's Avatar
    Join Date
    Oct 2005
    Posts
    20

    Another Homework Question

    I have a homework problem that I am not sure about. It deals with how input is read. The problem is:

    If input data are entered as follows
    10 20
    30 40
    50 60
    70 80

    and the input statements that read it are

    cin >> a >> b >> c;
    cin >> d >> e >> f;
    cin >> a >> b;

    What are the contents of the variables a,b,c,d,e, and f after the statements have executed.

    I believe the answer is:
    a=10
    b=20
    c=30
    d=40
    e=50
    f=60
    a=10
    b=20

    But what I don't understand is when will the values 70 and 80 be read? would there have to be an "g" and "h"?

    Thanks,

    Trekkie

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You can modify a variable after the first time you set its value, so it's possible for a variable's value to change. Just follow the code step by step, updating the values of a-f each time something changes. When you are done going through the code, you'll have the answer.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    The operator>>() is defined to read numbers sequentially. After it reads in a number, it marks where it stopped reading. If you use the operator>>() again, it begins where it left off.

    If you do this:
    Code:
    int num = 10;
    num = 20;
    What does num equal?

    Using the code:
    Code:
    cin>>some_variable_name;
    is just like assigning a value to the variable: you are directing Mr. C++ to read in a value from the keyboard(cin) and store it in the variable.
    Last edited by 7stud; 10-20-2005 at 12:32 AM.

  4. #4
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    Well what does the word variable mean: it means change. So theres no reason why a and b change change again. Also try compiling that code and put cout<< a etc afta each cin to what the variables hold.
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

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, 06: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, 09:22 PM
  5. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 03:57 PM