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)))

Recommended Answers

All 17 Replies

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 :)

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.clear();  // allows more reading
					cin >> next;
				}
				
			}
		}

: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

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.

In your loop:
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:

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

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.

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.clear();  // allows more reading
			 cin >> next;
			 return itemnum, price;
			 }
		}

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

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.clear();  // allows more reading
		 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;
  aDoubleAccumulator += someDouble;
  aCounter++;
}

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

hows this

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

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?

i thought thats how you read the next line?

while(!out_stream3.eof())  // reads file to end of file
			{
				  out_stream3 << p1 << p2 << count;
				  out_stream3 << p2;
				  p1 += p2;
				  p2++;
		   		  cin.clear();  // allows more reading
				  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.

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;

...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.

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.clear();  // allows more reading
			 cin >> next;
			 return itemnum, price;
			 }
		}

what does cin.clear() do?

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.

See this thread for more information.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.