timbomo -7

what is a way to get numbers in 2 diff columns one column is whole numbers and the other are numbers with decimal places

``````int price=' '; // i am attempting to grab a number that has a decimal but its not the correct way
int itemnum=' '; // i am attempting to grab a whole number but its not the right way

while((price== (price*1.00)) && (itemnum == (itemnum*1)))``````

lakarr

I'm not sure of the context of your code, but I can tell you that you have price declared as an integer, but you're using it as a double. If you could post more of your code, I might be able to help more :)

timbomo -7

sorry here you go
ifstream in_stream2;

``````in_stream2.precision(2);
while((price== (price*1.00)) && (itemnum == (itemnum*1)))
{
while (in_stream2 >> itemnum >> price) // gets itemnum and price
{
while (!in_stream2.eof())  // reads file to end of file
{
in_stream2 >> itemnum;
in_stream2 >> price;
price++;
curr_total= price++;
in_stream2 >> curr_total;
cin >> next;
}

}
}``````

Fbody 682

:confused: Why do you have 3 loops? You only need 1. The way you set those loops up, I don't even want to know what you'll get. A big mess, that much is certain.

``````ifstream inFile("input.fil", ios::in);
if (!inFile) return EXIT_FAILURE;

double decimalInput = 0.0;
int intInput = 0;

while (!inFile.eof()) {
// read a data pair from the file
inFile >> intInput >> decimalInput;

// process the data pair

}  // wend

// other misc. processing``````

lakarr

you...are making things very difficult for yourself. That outer loop is an infinite loop; your variables will always equal themselves times one. and the second loop doesn't need to be there either; you can trash that and just read the two values in before the last loop.

so if you get rid of the first loop you are left with one that will read through the file until said file ends, and that should be all you need.

you are reading in two values from a file ;
increasing one of those by one;
increasing it again and assigning it to a third variable;
not doing anything with that third variable, but overwriting it's contents with another value from the file;
having user input before the loop continues;

...what exactly are you trying to accomplish with this loop? It's not really doing anything :icon_neutral:

timbomo -7

i have to read numbers that are broken up into two columns, one column has whole numbers and the other is decimal numbers. and when it reads to the end of the line if the number has a decimal place than it is a price and if not than it is a itemnum. and i do this cuz i want to tally them up at the end to get the tota. well i thought thats wat i was doing

Fbody 682

You didn't tell us that you needed running totals. We are not mind readers, we don't know if you don't tell us.

You still can do it with just 1 loop. All you have to do is add an accumulator and a counter to the "process the data pair" part of my previous example.

timbomo -7

Hows this?

``````while (!in_stream2.eof())  // reads file to end of file
{
while((price== (price*1.00)) && (itemnum == (itemnum*1)))
{
in_stream2 >> itemnum >> price;
price++;
curr_total= price++;
in_stream2 >> curr_total;
cin >> next;
return itemnum, price;
}
}``````

VernonDozier 2,218

Line 8 makes line 7 pointless. You're overwriting what you just assigned, aren't you.

Fbody 682

Hows this?

``````while (!in_stream2.eof())  // reads file to end of file
{
while((price== (price*1.00)) && (itemnum == (itemnum*1)))
{
in_stream2 >> itemnum >> price;
price++;
curr_total= price++;
in_stream2 >> curr_total;
cin >> next;
return itemnum, price;
}
}``````

Why do you insist on putting `while((price== (price*1.00)) && (itemnum == (itemnum*1)))` in there? It does you absolutely no good at all, get rid of it....

Line 6 is an incrementer that makes your input data invalid as soon as you read it, it's not an accumulator. Line 7 is closer, but still not correct. This line makes your data even more invalid and doesn't keep a running total. All you need to do is read your input file then add the new value to the previously-stored value(s).

``````double aDoubleAccumulator = 0.0, someDouble = 0.0;
int aCounter = 0;

while (!in_stream2.eof())  // reads file to end of file
{
in_stream2 >> someDouble;
aCounter++;
}``````

And then we get to Line 8, but Vernon already covered that...

timbomo -7

hows this

``````while(!out_stream3.eof())  // reads file to end of file
{
out_stream3 << p1 << p2 << count;
out_stream3 << p2;
p1 += p2;
p2++;
cin >> next;
return p1, p2;
}``````

VernonDozier 2,218

Also, are you reading from a file or stdin or both. You have cin in lines 9 and 10. Are you reading input from the keyboard?

timbomo -7

i thought thats how you read the next line?

lakarr

``````while(!out_stream3.eof())  // reads file to end of file
{
out_stream3 << p1 << p2 << count;
out_stream3 << p2;
p1 += p2;
p2++;
cin >> next;
return p1, p2;
}``````

Okay, so you have a singe while loop that goes until you hit the end of the file; good.
Then you read in p1, p2, and...count? what is that for?
then you overwrite p2 and read in another value instead (That line shouldn't be there)
...what are you doing with p1 & p2? += is not an operator...I don't even know if that will compile.
then you add 1 to p2 is that what you were trying to do?
you really don't need the cin stuff to have the program keep reading from the file.
then you are returning p1 and p2.

from what you said earlier, you're reading from a file like this:

``````p1     p2
p1     p2
p1     p2
p1     p2
p1     p2``````

If that's true, what are you doing with the count variable?
also, if you're trying to return the total of the two, have a variable that will hold that amount.

ex)

``````int total=0; //the total
int num1=3; //some random int
int num2=4; //another random int

while (total<30) //while the total is less than 30...
{
total=total+num1+num2;  //add num1 and num2 to the total to get a new, cumulative total
}``````

[EDIT] Oh! and there's no reason to return anything...that will just end the program.

VernonDozier 2,218

i thought thats how you read the next line?

Read the next line from WHAT? Keyboard? If not, don't use cin.

``````cin.clear(); // allows more reading (reading from what?  Vern)
cin >> next;``````

viper657

...what are you doing with p1 & p2? += is not an operator...I don't even know if that will compile.

Stumbled upon this thread and thought I'd just clarify this for anyone else who might see it. In the code written above with "p1 += p2;" += is a valid operator, it's called a compound assignment operator. It does the same thing as typing "p1 = p1 + p2;". You can do the same thing with all the mathematical operators if I'm not mistaken.

dhruv_arora 5

Hows this?

``````while (!in_stream2.eof())  // reads file to end of file
{
while((price== (price*1.00)) && (itemnum == (itemnum*1)))
{
in_stream2 >> itemnum >> price;
price++;
curr_total= price++;
in_stream2 >> curr_total;
If an input stream is in an error state ( `streamName.good() == false` ), you need to remove the bad data from the stream. Once you have done that, you can call streamName.clear() to reset the error condition.