streams

my program is simply to ask the user for a sentence and then ask them for a character in that sentence and then output the position of the character but for some reason its skipping the get character part and outputting a random position.

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

int main()
{
	std::cout << "please enter a sentence. \n";
	
	std::string line;

	std::cin >> line;

	std::fstream input;

	input.open("input.txt");

	input << line;

	std::cout << "What character would you like to find? \n";
	
	char ch;

	std::cin >> ch;
	
	input.get(ch);

	int n = input.tellg();

	input.close();

	std::cout << "The charachter was in position: " << n 
		<< " of the file.";

	system("pause");
	return 0;
}
this is what I think you need http://www.cplusplus.com/reference/string/string/find/ and take a look at why your sentence is truncated http://stackoverflow.com/questions/4745858/stdcin-getline-vs-stdcin
thank you very much i learned what i needed
Topic archived. No new replies allowed.