Help Please

I was assigned to grab a text file names upquote.txt and turn it contents "a b c d e f" I tried this code here:

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
  #include <iostream>
#include <fstream>
#include <string>

using namespace std;

string upquoteLine, s;


int main() {

	ifstream infile;
	infile.open("upquote.txt");


	infile >> upquoteLine;

		getline(infile, upquoteLine);

		for (int i = 0; i < upquoteLine.length(); i++) {
			upquoteLine[i] = toupper(upquoteLine[i]);

		cout << upquoteLine << endl;
	}
	system("pause");

	return 0;
}


But all it gave me was this, and I'm trying to get it to output the "a" with the other letters.
b c d e f
B c d e f
B c d e f
B C d e f
B C d e f
B C D e f
B C D e f
B C D E f
B C D E f
B C D E F


If someone could guide me in the right direction that would be much appreciated.
1
2
infile >> upquoteLine;
getline(infile, upquoteLine);
¿what do you think you are doing there?
¿what value will have `upquteLine' after each command?
In that line of code it just calling the file object and putting it in a variable or placeholder then I'm getting it by calling it with a get line so I can output it out.


My ideal answer that I am supposed to be getting is "A B C D E F"
Last edited on
In that line of code it just calling the file object and putting it in a variable or placeholder then I'm getting it by calling it with a get line so I can output it out.


infile >> upquoteLine; extracts a space-delimited token from the stream represented by infile into the string upquoteLine.

getline(infile, upquoteLine); extracts the rest of the line into the string upquoteLine replacing the token extracted earlier.

One of those input extractions needs to go.
Okay so I took out infile >> upquoteLine; and it just gave me a loop that outputs:
A b c d e f
A b c d e f
A B c d e f
A B c d e f
A B C d e f
A B C d e f
A B C D e f
A B C D e f
A B C D E f
A B C D E f
A B C D E F

but when i take out getline(infile, upquoteLine); it outputs:
A

is there any way to fix both? or whichever one is convenient
this is your code properly indented
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <fstream>
#include <iostream>
#include <string>

using namespace std;

string upquoteLine, s;

int main() {
	ifstream infile;
	infile.open("upquote.txt");

	infile >> upquoteLine;
	getline(infile, upquoteLine);

	for(int i = 0; i < upquoteLine.length(); i++) {
		upquoteLine[i] = toupper(upquoteLine[i]);
		cout << upquoteLine << endl;
	}

	system("pause");
	return 0;
}
note how the cout << upquoteLine << endl; line is inside the loop, so you are outputting the whole line in every execution of the loop.
Wow, thank you haha. When I rearranged the code I literally screamed of joy. Now I just need to find out how to reverse it but I can probably figure it out myself. Thank You Cire, ne555
I figure out how to reverse the text with some research:

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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

string upquoteLine, s;


int main() {

	ifstream infile;
	infile.open("upquote.txt");

	getline(infile, upquoteLine);
	
	for (int i = 0; i < upquoteLine.length(); i++) {
		upquoteLine[i] = toupper(upquoteLine[i]);
	}
	reverse(upquoteLine.begin(), upquoteLine.end());

	cout << upquoteLine << endl;
	system("pause");

	return 0;
}
Topic archived. No new replies allowed.