CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Aug 2012
    Posts
    1

    [RESOLVED] Reading integers from a data file, and using the integers in formulae

    oo
    Last edited by mcurley104; August 28th, 2012 at 03:33 PM.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Reading integers from a data file, and using the integers in formulae

    So what have you already done? And what part of the code is "hard" for you?
    Victor Nijegorodov

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Reading integers from a data file, and using the integers in formulae

    Quote Originally Posted by mcurley104 View Post
    ...
    pretty sure there is some problems with that but thats been my best attempt at starting it
    Why are you "pretty sure"?
    Did you try to compile this code?
    Victor Nijegorodov

  4. #4
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: Reading integers from a data file, and using the integers in formulae

    Code:
    vector<int> x,y;
    int x,y,i=1,lines;
    You defined x and y twice.

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Reading integers from a data file, and using the integers in formulae

    Quote Originally Posted by Skizmo View Post
    Code:
    vector<int> x,y;
    int x,y,i=1,lines;
    You defined x and y twice.
    On the other hand there are some undeclared variables:
    • sum_of_x;
    • sum_of_y;
    • c1[ ];
    • c2[ ];
    Victor Nijegorodov

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Reading integers from a data file, and using the integers in formulae

    You must fix all the errors the compiler shows you. Use MSDN to obtain info about compiler errors and their possible causes.
    Victor Nijegorodov

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Reading integers from a data file, and using the integers in formulae

    Quote Originally Posted by mcurley104 View Post
    yeah i am in the middle of doing that, i can't think of what the formula would be to add all of the x values together, any suggestions?
    There is no formula. You add them up in a loop, adding to a running total.

    Regards,

    Paul McKenzie

  8. #8
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Reading integers from a data file, and using the integers in formulae

    Quote Originally Posted by mcurley104 View Post
    Thanks Paul, i have decided to re-do the structure of the program. this is what i have so far
    You need to use code tags when posting code.
    Code:
        float X^2;
    What is that supposed to do?
    i am just not sure how i could implement a loop to find the sum values for "X, Y, XY and X^2" from the data file?
    You don't "find the sum" by doing what you're doing now. Where is the attempt to write the loop?

    You're supposed to read in one line at a time, and for each line you read, you add that value to a running total. When all of the numbers are read, those totals you have are the addition of the values.

    Have you ever used a simple calculator? How do you add 10 numbers? Is there a huge "ADD" button that adds all the numbers in one go? No there isn't. What you do is that you "loop" -- you enter a number, you press the '+' key, you enter the second number, you enter the '+' key, etc. The program should be mimicking how you enter numbers into a calculator. The total starts out at 0, and then you read the next number and add to the total. By the time all the numbers are read, you have your grand totals.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; August 27th, 2012 at 12:48 PM.

  9. #9
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Reading integers from a data file, and using the integers in formulae

    Quote Originally Posted by mcurley104 View Post
    ... when i compile this it says the "cin"'s and "cout"'s are undeclared identifiers, do you see where i could be getting this problem?
    In every such a case you should go to MSDN (in the case of cin it would be cin) and see what header files you have to include and what namespaces you have to use.
    Besides, as Paul already mentioned, you have to use Code tags while posting code snippets. Please, read the Announcement: Before you post....
    Victor Nijegorodov

  10. #10
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Reading integers from a data file, and using the integers in formulae

    here is my attempt to put in a loop,
    You still did not put your code in code tags.
    first time i have ever done a loop,
    Why in all of your loops you're outputing values? Aren't you supposed to be inputing values in the loop?

    Step back and take a look logically at what you wrote. Does it make sense to output 21 times some unknown value? Look at this using psuedo-code (and logic).
    Code:
    0) set all of your totals to 0
    1) Open the file
    2) While there is data in the file
         2a) Read next line in the file
         2b) Take the data from that line and add it to the totals
         2c) loop again (go back to step 2)
    3) End.
    Do you see how logically this makes sense? Compare those words above with what you wrote? Did what you write have any resemblance to those steps? After step 3), those totals are going to have the total of the numbers.

    Regards,

    Paul McKenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured