Reading a text file to create a double vector

Hi everyone,
I'm a beginner in using C++. I would like to read in C++ 2015 a .txt file like this:

A 1.1
B 0.006
C 2.15
D 2.90

So I have names (characters or strings) in the first column and numbers in the second one.
I want to read only the second column and in particular to create double variables in order to obtain in C++ for example:

double a = 1.1;
double b = 0.006;
double c = 2.15;
double d = 2.90;

(Rows abovementioned are just a schematization about what I want)

I have difficulties because of the difference of the type of data in the second and first columns.
Have you any suggestion about a simple code that can do it?

I tried with the following one but it is wrong because C++ returns strings turned into double and not the numbers of the second column as I want.




#include <iostream>
#include <fstream>
#include <string.h>


using namespace std;
int main() {

double vector[8];
ifstream inFile;
inFile.open("written_file.xls");

int i = 0;

while (inFile.good()) {
inFile >> vector[i];
i++;

}

for (int i = 0; i<8; i++) {
cout << vector[i] << endl;

}

getchar();
return 0;

}


I hope in a useful help.
Thank you!
I have a suspicion that you are not stating what your assignment actually asks, but rather your mangled reading of it.

Amongst other things there is no such thing as C++15 and it is highly unlikely that your pure text data file will have an extension of .xls.

So, state your original assignment task, please.



What, if anything, do you want from the letters in the first column? You are not going to create program variables on the fly, although you could put them in, for example, char variables. Or you may not need them at all; who knows?

When someone uses the loose term "double vectors" when talking about C++ they are probably meaning
vector<double> varname;
In this instance this would probably be populated by repeated use of push_back(), rather than assuming a fixed size.

Your statement for the file input is not appropriate, as whether inFile is "good" or not will not be known until you have tried to read the next variable, by which time you will be committed to incorrectly increasing a counter. Use something like
1
2
3
4
5
6
7
8
9
char ch;
double value;
vector<double> V;
ifstream inFile( "input.txt" );
while (inFile >> ch >> value ) 
{
   V.push_back( value );
   // ... plus anything you want to do with the character ch
}



PLEASE PLEASE PLEASE USE [code][/code] TAGS - first item in the format menu.
Hello MakeMeFeel,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

It makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



The Line double vector[8]; is a bit misleading. Are you wanting to create an array called vector or a "vector" of something. You could create a vector of "standard pair" being a "string" or "char" and a "double" or create a struct and store that in a vector or an array.

Include the header file <vector> and the definition might be
std::vector<std::pair<std;:string, double>> varibleName; or std::vector<structName> varibleName;.

http://www.cplusplus.com/reference/utility/pair/pair/
http://www.cplusplus.com/reference/vector/vector/

I noticed that you have the header file "string.h" this is a C header file. For a C++ program you want the header file "string".

If you do not need the letter in the file, first field, you could define std::string junk; or if it is a single lettter char junk{};. Then read the file as inFile >> junk >> vector[i]; and choose another name other than "vector" as this could be a problem or at least confusing.

Your program would work better this way if this is what you need:
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
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <fstream>
#include <string>
#include <chrono>
#include <thread>

//using namespace std;  // <--- Best not to use.

int main()
{
	constexpr size_t MAXSIZE{ 8 };

	double numbers[MAXSIZE]{};
	std::string junk;
	std::ifstream inFile("Data.txt");
	//inFile.open("Data.txt");

	if (!inFile)
	{
		std::cout << "\n File " << "\"Data.txt\"" << " did not open" << std::endl;
		std::this_thread::sleep_for(std::chrono::seconds(3));  // <--- Needs header files chrono" and "thread".
		return 1;
	}

	int count = 0;  // <--- Renamed to use in for loop

	while (inFile >> junk >> numbers[count])
		count++;


	for (int i = 0; i < count; i++)  // <--- Should be "count" from the while loop not 8. 
		std::cout << numbers[i] << std::endl;

	getchar();

	return 0;

}


Hope that helps,

Andy

Thank you very much Andy! As I already said I'm a beginner in C++ and even in using this forum. For this reason I didn't use Code Tags. I understood your explaination, a little less the role of the string junk.
Now the script works, I don't know how to say thank you!

Thank you Lastchanche too, even if the few sociable response.

Hello MakeMeFeel,

My bad. I should have made the "std::string junk;" just "char junk{};".

The point is that in while (inFile >> junk >> numbers[count]) the variable junk is just reading the first letter, but does nothing with it. It is just a way to get past the letter to get to what you need.

Hope that helps,

Andy
Last edited on
Topic archived. No new replies allowed.