Problem with file data manipulation.

I'm trying to create a program that opens a .txt document with weight in kilograms separated by a white space and converts it to pounds which then displays it to the user.

I keep getting an error saying "error C2676: binary '*' : 'std::string' does not define this operator or a conversion to a type acceptable to the predefined operator"

This is the code I have.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
#include <fstream>
using namespace std;

int main()
{
	string studentData;
	double pounds;
	ifstream dataFile("C:\\Users\\Joe\\Desktop\\weightKilo.txt");
	if (dataFile.is_open())
	{
		while (dataFile.good())
		{
			getline(dataFile, studentData);
			string kilo = studentData * 2.2;
			cout << kilo << endl;
		}
		dataFile.close();
	}
	else cout << "Unable to open/locate the selected file." << endl;
	return 0;
}
studentData is a string. You cant multiply string with numbers.

studentData * 2.2;

Why are you using string to get weights? You should use integer or float/double for that, since it is numbers.
Last edited on
I changed it to float, and now an error appears with the getline function. What do you suggest I do?
You cant use getline with float. Could you please post your text file here?

Also, please dont double post. One section only. Remove the one on general.
Last edited on
the text file is just:

80
72
69
70
82
Then you could do something like.

Loop 5 times, since its 5 numbers.


1
2
3
4
5
6
7
	float weight;
	float pounds = 0;
	for (int i = 0; i < 5; i++)
	{
		dataFile >> weight;
		pounds = pounds + (weight *2.2)
	}


This code will combine (plus together) all the pounds. Is that what you want?
Last edited on
im not exactly sure how to implement that with the file
How to implement what with the file?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
float weight;
	float pounds = 0;
	ifstream dataFile("C:\\Users\\Joe\\Desktop\\weightKilo.txt");
	if (dataFile.is_open())
	{
		while (dataFile.good())
		{
			for (int i = 0; i < 5; i++)
			{
				dataFile >> weight;
				pounds = pounds + (weight *2.2)
			}
		}
		dataFile.close();
	}
	else cout << "Unable to open/locate the selected file." << endl;
Last edited on
i tried using that code and it just gives me a blank program when i run it
Yeh well. What are you expecting it to give you? Youre not cout-ing anything. You're just taking all the kilos and converting them to pounds, and placing them all in the pound variable.

If you std::cout << pounds << std::endl; You will get all 5 weights in pounds together.
thank you sir
My pleasure :)
Is it possible to display the original values from the file before the loop as well?
Yes. Just add this line between

1
2
dataFile >> weight;
pounds = pounds + (weight *2.2)


Add:

std::cout << "Weight " << i+1 << ": " << weight << " kg" << std::endl;
Last edited on
Dude, thank you so much for this. I appreciate all the help youve given me
for (int i = 0; i < 5; i++)


as file can be variable length, use while(!dataFile.eof()) instead.
Topic archived. No new replies allowed.