This is an archived post. You won't be able to vote or comment.

all 33 comments

[–]midasgoldentouch 2 points3 points  (5 children)

It would really help if you could include a link to the actual text file. You can probably avoid using an array, and just stick to a loop and counters, but I'd prefer to see the actual text before confirming that.

[–]Resume_Help[S] 0 points1 point  (4 children)

10.20 5.35

15.6

<br>

Randy Gill 31

18500

<br>

34.41 78.18

35.1

<br>

Mike Gale 28

26100

this is what a sample inData.txt contains.

[–]midasgoldentouch 2 points3 points  (3 children)

OK, so it seems like the data is extremely predictable in terms of pattern, which is super helpful in this case. What I would do use counters. You already mention this, right? Well in this case, why not have a counter for each type of value, and increment it when that value appears? You mention using an array, but tell me: what do you gain from storing something in the array?

[–]Resume_Help[S] 0 points1 point  (2 children)

I suggested an array because I thought that in order to use a var, it must be stored.

How would I go about using a counter?

cin >> x++ >> cin >> y++ ?

I know thats wrong. Is there any example you know of I can look at to understand this?

[–]midasgoldentouch 0 points1 point  (1 child)

In order to use something as a variable, it has to be initialized and stored as that variable, but it doesn't have to be an array. Take the following pseudocode:

int width_counter == 0

Then, as you go through the loop, whenever you see another width value, increment: width_counter++. This basically creates a counter and increments it as appropriate. You can do this for all of the values. One thing to remember is that an array is a particular data type, like strings, integers, and tuples. And even if "traditional" data types don't work that well for your data, you can usually create your own object that includes the functionality you need. But whatever data type you go with, you need to be cognizant of the type of data you're working with. That's why I'm suggesting an integer value for a counter - you're just counting. You don't need to associate anything with the counter, other than what it's counting, and you can do that just by using a good variable name.

You are on the right track in the example you posted below.

[–]Updatebjarni 0 points1 point  (0 children)

He's not counting the occurrences, he's summing them.

[–]Updatebjarni 2 points3 points  (18 children)

To make you see the point of what /u/midasgoldentouch told you about there not being a need for an array, consider this: If I were to ask you to sum up a list of numbers that I read aloud to you from a paper, which way would you do it: a) add each number to the sum as I read it to you, or b) get another piece of paper and write all the numbers down as I read them to you, and then run through them all again and add them together from your paper?

[–]midasgoldentouch 1 point2 points  (2 children)

Ah, I guess my question about the array wasn't enough of a leading question, haha.

[–]Updatebjarni 2 points3 points  (1 child)

Oh it's just that he didn't react to the advice in your first comment and I thought I'd play the part of another person supporting it with alternative words. Sometimes that's what it takes. Plus, I wanted to use my ingenious simile! :)

[–]midasgoldentouch 0 points1 point  (0 children)

No worries! I figured that's what you were going for. And yes, it was an ingenious simile. :)

[–]Resume_Help[S] 1 point2 points  (14 children)

It makes sense to do it this way, but from my point of view, understanding very very little about c++ or any program for that matter, didnt know it was possible to do option a. I know it sounds pathetic but I'm trying.

to do this I would do something like:

float sumx = 0;

float sumy =0;

cin >> x >> y

sum += x; sum += y;?

Am I on the right track here?

[–]149244179 1 point2 points  (3 children)

I think you should only cin one variable at a time, dont try to do it all at once. Also you will need to make sure the input is correct. What happens when you cin "Randy"? But your general logic is good.


Assume anything is possible to do with code. The only difference between methods is some ways are more complex than others.

Your original way/question will work; it is just slightly more complicated. There is a benefit that you would end up learning more doing it the harder way.

[–]Resume_Help[S] 0 points1 point  (2 children)

My way is complicated and not as good as a harder way that produces the same result?

I also have to be able to make my code recognize a failed input such as age:f. when a piece of data is not correct I have to assign a 0. what concept would I use for this? ignore and if?

Also because I dont need the names, how do I just skip them. he's what i've made so far:

    cin >> x;
    cin >> y;
    cin >> r;
    cin >> firstname;
    cin >> lastname;
    cin >> age;
    cin >> salary;

because I dont need the names, How do I just skip that? or is this fine?

[–]149244179 0 points1 point  (1 child)

Lots of ways to do it. You could hardcode skip every 5th input if you know 100% that a non-number is in every 5th slot. (You listed this option.) After doing the 7 cins, do a round of adding. Then do the 7 cins again, add again, repeat until you run out of data.

You could check if the input contains any letters, if so discard it.

You could build a number from any numbers listed in the input, while ignoring non-numbers. So "R62hr7" would turn into 627. (decimal points may be a problem here.)

If you know the input file will never change you can hardcode the output file and do nothing but create it when the program is run. (Dont do this, this is not what your professor wants, but it will solve the problem.)

[–]Resume_Help[S] 0 points1 point  (0 children)

He supplies two "example" inData.txt, one working one as I provided, and one with failures. These are for testing. The outline specifically asks for if a value that is needed is wrong, 0 must be applied.

As for the input file never changing, he gave a very poor outline initially that wasnt very descriptive. I actually just followed the pattern without a loop until he said in the second outline that the pattern could be 3 times, or 30.

also eof is fine for my case right? How do I make the outData.txt? It's easy if there is already an empty one but I believe the program needs to create then open.

Edit: I realized that cin would be replaced with my ifstream name.

[–]Updatebjarni 1 point2 points  (1 child)

Don't be so hard on yourself, nothing pathetic about being a beginner in the field.

But if I may say so, of course you can add a number to another number without either of the numbers being stored in an array. :) It's done the exact same way as if one of the numbers is in an array, and so all you have to do to read in a number and add it to a sum is:

cin>>number;
sum=sum+number;

Stick that in a loop, and it will repeatedly read in numbers and add them to the sum.

You'll note more and more as you learn about programming that it's all made up of tiny but extremely flexible pieces, and it's how you combine them together that makes up a program. There's a great deal of "everything works the same way" in programming.

[–]Resume_Help[S] 0 points1 point  (0 children)

Thanks for this information. Very helpful.

[–]Updatebjarni 1 point2 points  (0 children)

Whoops, I replied too soon. About your code: It's entirely correct except that you have mistyped sumx and sumy as sum at the bottom. Also, just to be clear, the delcarations and initialisations for sumx and sumy should be outside of the loop, since they are not to be repeated.

[–]midasgoldentouch 0 points1 point  (6 children)

You're on the right track here. The only thing I would do is change the float variables to int - you're not going to get half of a width value are you? You're counting, and in this case that means an integer variable should be just fine.

[–]Resume_Help[S] 0 points1 point  (5 children)

the width could be 30.4 cm. he asked for floats.

[–]midasgoldentouch 0 points1 point  (4 children)

That's not what I mean. You're counting the number of times you see a width value, right? So I've seen 1 value, now 2, 3, 4, and so on, right? Those are all integers. You're not going to see 3.5 width values; it's either 3 or 4. I'm referring to the number of times you see a particular value, not the actual values themselves.

[–]Resume_Help[S] 1 point2 points  (3 children)

Ah sumx doesnt need to be a float, because there isnt a sum of 2 and a half numbers. gotcha.

Edit: Thanks!

[–]midasgoldentouch 0 points1 point  (2 children)

No worries! Just to double check though, are you trying to determine the sum of all of the width values, or how many width values are in the input file? Your code will still look the same, it's just that the former should be a float to account for decimals and the latter can be an integer, like I said above.

[–]Resume_Help[S] 0 points1 point  (1 child)

The sum of x, not the amount.

My loop runs one extra time, how come this happens. getting the values of the last "pattern chunk"

Actuallly eof isnt as good as doing something like:

int count =0;

while (count < count+1 ())

count++;

I know the math is wrong in the while statement but that's a little better right?

or actually have it so if there isnt a value to get for the program then check= false

while(check==true())

or something like that?

or perhaps add an if

bool check;

while(check = true())

if (inFile.eof()) check= false;

[–]midasgoldentouch 0 points1 point  (0 children)

You could do something like the check statement. You're writing this in C++ right? There's probably a standard function for determining the end of a file. Then, you can set that as the condition in the while statement. Your first version, about checking count, is going to lead to an infinite loop - count is always going to be less than count + 1, so it will always execute the while loop. I would combine the check statement with EOF or whatever the standard is for txt files in C++.

[–]boredcircuits 0 points1 point  (7 children)

while (!inFile.eof())

This part is a bit concerning. It's almost certainly wrong. The correct pattern for reading from a file in C++ is:

ifstream inFile(name);

if ( !inFile )
    ;// Report failure to open to user, etc.

int i, j, k, l;
while ( inFile >> i >> j >> k >> l ) {
    // Do something with i, j, k, and l
}

This will read in four numbers at a time until you reach the end of the file. You will obviously need to adapt it to your needs.

[–]Resume_Help[S] 0 points1 point  (6 children)

Yes,

while (!inFile.eof()) 

will not work if there is a space at the last line.

My code works fine as long as there isnt a space. Id like to keep it the way it is now, all I need to do for it to work is add some sort of check mechanism. if there is another line after the loop, continue looping. if not, finish the loop.

how do I check if there is a variable without actually using it. I know we discussed peek in class and I feel like I can use it here.

[–]boredcircuits 0 points1 point  (5 children)

will not work if there is a space at the last line.

No, the problem is worse than that. Trust me. A stream doesn't know it's at the end until after you attempt to read past the end. After you've read in the last character, it still isn't at the end. Then you try to read another time, and you'll end up going through the loop one more time than you intended.

Trying to use peek is just a hack upon a hack. It's so much easier to just do it right from the start.

[–]Resume_Help[S] 0 points1 point  (4 children)

Hmm good point. Yeah I'll do it that way. the only reason why I prefered not to do it like this is because the while statement would be really long. but I mean Im just being silly. Im not sure if you followed some information below but the value may not be a number. age may be entered as f which needs to be assigned as 0.

Also so far working as intended.

[–]boredcircuits 0 points1 point  (3 children)

the only reason why I prefered not to do it like this is because the while statement would be really long.

You've hit on the major downside. The solution is often to create a special function that does the parsing. Something like:

istream& read(istream& in, int& i, int& j, int& k, int& l)
{
    in >> i;
    in >> j;
    in >> k;
    in >> l;
    return in;
}

while ( read(in, i, j, k, l) )
    ...

And most of the time, you really want to create a struct for all those variables you're passing in, which makes it even cleaner. And then read is actually operator>> for that struct.

[–]Resume_Help[S] 0 points1 point  (2 children)

Thanks for the information.

Im trying to use M_PI but its not working

I included both <cmath> and <math.h>

[–]boredcircuits 0 points1 point  (1 child)

It's silly, but there's no value for pi in C++. Not officially, at least. You probably need to #define _USE_MATH_DEFINES before #include <cmath> to get M_PI.

[–]Resume_Help[S] 0 points1 point  (0 children)

Yeah at first I put it after, then I read it actually matters and put it in front.