Nov 27, 2016 at 9:09am Nov 27, 2016 at 9:09am UTC
im quite new to c++ and this is my problem
im writing a program where user can input a string and two integers of their choice
the inputs will then write into a file
let's assume that text file, "example.txt" is already exist with the contents of
1 2 3
hello world 1 2
i like you 3 4
c++ programming 9 8
after the user inputs, the user is able to search strings in the text file.
for example, if the user search "hello world", it will print:
my problem is that, i cant search strings with white spaces in my text file
this is my program code so far :
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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
struct example {
string title;
int x, y;
};
int main(){
example test;
test.title = "" ;
test.x = 0;
test.y = 0;
bool found = false ;
fstream testing;
string search;
if (testing.fail()){
cout << "example.txt file not found!" << endl;
return -1; //abnormal termiation. if something fail
}
testing.open("example.txt" , ios::out | ios::app);
cout << "please enter a string" << endl;
testing << "\n" ;
getline(cin, test.title);
testing << test.title;
cout << "please enter an integer" << endl;
cin >> test.x;
testing << "\t" << test.x;
cout << "please enter another integer" << endl;
cin >> test.y;
testing << "\t" << test.y;
testing.close();
cout << "what do you like to search?" << endl;
cin.ignore();
getline(cin,search);
testing.open("example.txt" , ios::in);
while (!testing.eof()){
testing >> test.title;
if (test.title == search){
testing >> test.x >> test.y;
cout << test.title << endl;
cout << test.x << endl;
cout << test.y << endl;
found = true ;
}
}
testing.close();
if (found != true ){
cout << "not found" << endl;
}
return 0;
}
Last edited on Nov 27, 2016 at 9:13am Nov 27, 2016 at 9:13am UTC
Nov 27, 2016 at 9:24am Nov 27, 2016 at 9:24am UTC
Source code
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 39 40 41 42 43 44 45 46 47 48 49
#include<iostream>
#include<fstream>
#include<string>
#include<stdlib.h>
using namespace std;
int main()
{
int num1,num2,choise;
string name,name1;
ifstream data;
data.open("data.txt" );
if (data.fail()) // if file is not avaliable
{
cout<<"\nError Opening The File\n" ;
cin.get(); // wait for any key to return
exit(1); // quit
}
while (true )
{
cout<<"\nEnter : " ;
getline(cin,name1);
while (getline(data,name)>>num1>>num2)
{
if (name==name1)
{
cout<<name<<endl;
cout<<num1<<endl;
cout<<num2<<endl;
}
}
data.clear(); // clear
data.seekg(0,ios::beg); // start form begining
cout<<"\n1 to try again : " ;
cin>>choise;
if (choise==1)
{
system("cls" );
cin.ignore();
continue ;
}
else
break ;
}
return 0;
}
arange your data in this form
hello world
1
2
i like you
3
4
c++ programming
9
8
Last edited on Nov 27, 2016 at 9:28am Nov 27, 2016 at 9:28am UTC
Nov 27, 2016 at 9:26am Nov 27, 2016 at 9:26am UTC
std::getline has 3 arguments, stream, destination variable and separators. Newline is by default a separator for std::getline, space will be taken.
std::getline(file, line, '\n' );
Last edited on Nov 27, 2016 at 10:21am Nov 27, 2016 at 10:21am UTC
Nov 27, 2016 at 10:09am Nov 27, 2016 at 10:09am UTC
firstly im sorry to @ashton vern bcoz im not going to respond for your quest but
im here to say something to @Golden Lizard .
i cant rply your message, this web not allow me to do so maybe bcause im new here ,
thank you for respond to my message , i really want to ask your help regarding to my assignment . any other way that i can connect to u ? you can just rply me through pm . thanks .
sorry again ashton vern
Nov 27, 2016 at 11:03am Nov 27, 2016 at 11:03am UTC
oh oh, thank you so much bird1234!!
as for golden lizard, thank you so much for the reply too!! i finally understand how to use getline properly
zuhairah, its ok :)