Quantcast
Channel: Visual C forum
Viewing all articles
Browse latest Browse all 15302

How to read from a text file in a windows form application (C++)

$
0
0

Hi, i'm new to the whole windows form application in C++. I want to make a simple app that prompts the user to enter his name. The app should open a text.txt file and output the corresponding mobile number to that name.

Here is what the .txt file looks like : 
Ahmed  01148220333
John 023837427
David 119922248

Here is what an equivalent C++ console code would look like 
int main ()

{
ifstream IN; 
string Filename, number,name;
cin>>name;
IN.open ("text.txt");
if (IN.fail())

return -1; 

IN>>Filename; 
while (!IN.eof())
{
IN>>number;
if (Filename==name)
{
cout<<number<<endl;
IN.close();

return 0; 
}
IN>>Filename;
}
cout<<"Name does not exist"<<endl;
IN.close();
return 0;

}

 

Viewing all articles
Browse latest Browse all 15302

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>