Skip to content

Commit 4c61fad

Browse files
Read two lines of number into two separate vectors.
1 parent 345c266 commit 4c61fad

File tree

4 files changed

+102
-6
lines changed

4 files changed

+102
-6
lines changed

.vscode/settings.json

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,59 @@
5555
"C_Cpp_Runner.useLeakSanitizer": false,
5656
"C_Cpp_Runner.showCompilationTime": false,
5757
"C_Cpp_Runner.useLinkTimeOptimization": false,
58-
"C_Cpp_Runner.msvcSecureNoWarnings": false
58+
"C_Cpp_Runner.msvcSecureNoWarnings": false,
59+
"files.associations": {
60+
"atomic": "cpp",
61+
"bit": "cpp",
62+
"cctype": "cpp",
63+
"charconv": "cpp",
64+
"clocale": "cpp",
65+
"cmath": "cpp",
66+
"compare": "cpp",
67+
"concepts": "cpp",
68+
"cstddef": "cpp",
69+
"cstdint": "cpp",
70+
"cstdio": "cpp",
71+
"cstdlib": "cpp",
72+
"cstring": "cpp",
73+
"ctime": "cpp",
74+
"cwchar": "cpp",
75+
"exception": "cpp",
76+
"format": "cpp",
77+
"fstream": "cpp",
78+
"initializer_list": "cpp",
79+
"ios": "cpp",
80+
"iosfwd": "cpp",
81+
"iostream": "cpp",
82+
"istream": "cpp",
83+
"iterator": "cpp",
84+
"limits": "cpp",
85+
"locale": "cpp",
86+
"memory": "cpp",
87+
"new": "cpp",
88+
"ostream": "cpp",
89+
"sstream": "cpp",
90+
"stdexcept": "cpp",
91+
"streambuf": "cpp",
92+
"string": "cpp",
93+
"system_error": "cpp",
94+
"tuple": "cpp",
95+
"type_traits": "cpp",
96+
"typeinfo": "cpp",
97+
"utility": "cpp",
98+
"vector": "cpp",
99+
"xfacet": "cpp",
100+
"xiosbase": "cpp",
101+
"xlocale": "cpp",
102+
"xlocbuf": "cpp",
103+
"xlocinfo": "cpp",
104+
"xlocmes": "cpp",
105+
"xlocmon": "cpp",
106+
"xlocnum": "cpp",
107+
"xloctime": "cpp",
108+
"xmemory": "cpp",
109+
"xstring": "cpp",
110+
"xtr1common": "cpp",
111+
"xutility": "cpp"
112+
}
59113
}

programming4.cpp

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,72 @@
66
#include <iostream> //do I need it?
77
#include <fstream>
88
#include <sstream> //string stream -- allows me to treat a string as if it is a stream
9+
#include <vector>
910

1011

1112
int main()
1213
{
1314
//Read two lines of numbers in input.txt and store them into two vectors
1415
//std::ifstream -- standard input file stream. Read data from a file.
1516
std::ifstream inputFile("input.txt");
16-
std::string word;
17+
std::string line;
18+
std::vector<double> vector1;
19+
std::vector<double> vector2;
20+
1721

1822
if(inputFile.is_open())
1923
{
20-
while(inputFile >> word)
24+
//Read the first line into first vector.
25+
//(Use while loop if want to read continuously)
26+
27+
if(std::getline(inputFile, line))
28+
//getline function reads an entire line of text from a stream
29+
//(in this case, that stream is inputFile)
2130
{
22-
//while input file was still able to read word-by-word
23-
std::cout << word << std::endl;
31+
std::stringstream ss(line); //treat string like a stream. Able to extract tokens using >>.
32+
double number;
33+
while(ss >> number)
34+
{
35+
vector1.push_back(number);
36+
}
37+
2438
}
25-
39+
40+
//Read the second line //A few thought: maybe create a separate function to read each line
41+
if(std::getline(inputFile, line))
42+
{
43+
std::stringstream ss(line);
44+
double number;
45+
while(ss >> number)
46+
{
47+
vector2.push_back(number);
48+
}
49+
}
50+
2651
inputFile.close(); //closes the file
2752
}
2853
else
2954
{
3055
std::cout << "File could not be opened!" << std::endl;
3156
}
3257

58+
//Print out vectors to check.
59+
std::cout << "My Vector1: " << std::endl;
60+
61+
for(const double& num: vector1)
62+
{
63+
std::cout << num << " ";
64+
}
65+
66+
std::cout <<std::endl;
67+
68+
std::cout << "My Vector2: " << std::endl;
69+
70+
for(const double& num: vector2)
71+
{
72+
std::cout << num << " ";
73+
}
74+
3375
return 0;
3476

3577
}

programming4.exe

89.5 KB
Binary file not shown.

programming4.obj

184 KB
Binary file not shown.

0 commit comments

Comments
 (0)