reading from existing file name

+2 ragmar thomas · August 5, 2014
#include <iostream>
#include<fstream>
using namespace std;
int main()
{
    string choice, meaning, word, yesOrno;
    char add[20];
    cout << "\n\n\n\n" << endl;main:menu:
    cout << "          search word -s" << endl;
    cout << "          add words -a" << endl;
    cout << "          to exit -q" << endl;
    cin >> choice;cin.ignore();
    while(choice!="q")
{
            if(choice=="a")
{
        cout << "you want to add the word <y-for yes> or <n-for no> " << endl;
    cin >> yesOrno;cin.ignore();
    if(yesOrno=="y")
{
    ofstream file;
    file.open(add);
    cout << "the  word you want to add please enter" <<endl;
    cin.getline(add, 20);
     file.open(add);
    cout << "please enter the meaning of the word" << endl;
    getline(cin, meaning);
    file << meaning;}
    else
    if(yesOrno=="n"){goto main;}}
    else
    if(choice=="s")
{
            char sch[20];
    ifstream file;
    cout << "please enter the word you want to search for: ";
    cin.getline(sch, 20);
  file.open(add);
     if(add==sch
){/*here is supposed to check the word saved previously
and the new word that the user want to find in the folder but it want may*/
   cout << "the meaning of you word is " << endl;
   while(file >> sch)
{
        getline(file, meaning);
        cout << meaning;}
     }else if(add!=sch)
{
    cout << "word not found" << endl;goto menu;}}}
}



here in this code when searching a word from previously saved words from the user it won't retrieve it from the folder were the names of the files are stored even if i open the word and i tried to check whether the word that the user want to find exist or not. i'm not sure about the particular code just help me how to fix this problem(the problem is in this particular code)
if(choice=="s"){
            char sch[20];
    ifstream file;
    cout << "please enter the word you want to search for: ";
    cin.getline(sch, 20);
  file.open(add);
     if(add==sch)
{
    cout << "the meaning of you word is " << endl;
   while(file >> sch)
{
        getline(file, meaning);
        cout << meaning;}
     }else if(add!=sch)
{
    cout << "word not found" << endl;goto menu;}}}
note: make sure to save a word before searching by add it with "a" from the menu.
thanks in advance.
O boy, the problem with the insertion or extraction operator is not my problem this problem was sustaining for a while hopefully Bucky or his team will deal about it .

Post a Reply

Replies

Oldest  Newest  Rating
+1 Mathias Frits Rørvik · August 5, 2014
You can easily replace the goto with a while or do while loop.
do {
//Stuff
} while (yesOrno != "y");


I can't help you any further until you clean up the formatting of the code.
0 ragmar thomas · August 7, 2014
#include <iostream>
#include<fstream>
#include<cstdlib>

using namespace std;

int main()
{
   string choice, meaning, word, yesOrno, add, sch;
   cout << "\n\n\n\n" << endl;

   cout << "          search word -s" << endl;
   cout << "          add words -a" << endl;
   cout << "          to exit -q" << endl;

   while(choice!="q")
   {
      cout << " please enter your choice: ";
      cin >> choice;
      cin.ignore();

      if(choice=="a")
      {
       do{
            cout << "you want to add the word <y-for yes> or <n-for no> " << endl;
            cin >> yesOrno;
            cin.ignore();

         if(yesOrno=="y")
         {
            ofstream file;
            cout << "the  word you want to add please enter" <<endl;
            getline(cin, add);
            file.open( add.c_str() );
            cout << "please enter the meaning of the word" << endl;
            getline(cin, meaning);
            file << meaning;
         }
         else if(yesOrno=="n")
         {
            break;
         }
      }while(yesOrno!= "y");
         }

      else if(choice=="s")
      {
         ifstream nfile;
         nfile.open( add.c_str() );
         cout << "please enter the word you want to search for: ";
         getline(cin, sch);

            if(add==sch)
            {
               cout << "the meaning of you word is " << endl;
                  nfile >> meaning;
                  cout << meaning << endl;
            }
            else if(add!=sch)
            {
               cout << "word not found" << endl;
            }

      }
   }
}





here the add variable is like the file name that's how the word in a sense the file can be opened and compared from the user choice input that is the only  way the file can be opened and i think 'm close but not enough it works partially when the user enter a word and search  it immediately that is the only way it works if u have any suggetions please forward it means a lot. thanks a lot.
  
0 ragmar thomas · August 8, 2014
Linguist llama thank u for your patience with my idiocy, i have been hovering around it  probably 50X but i can't  see it even if you told me more than twice, damn thank you very much, and i'll try to correct my punctuations or my writing skill. thank u again u make my day. PEACE!
0 ragmar thomas · August 8, 2014
yep sure, i'll save the words first before searching them, thanks dude for your comment  see you around. Peace!
0 Mathias Frits Rørvik · August 5, 2014
This code is almost unreadable, brackets aren't showing up properly and you have you have placed curly braces all over the place and you have used goto, which you should never ever use.
0 ragmar thomas · August 5, 2014
thanks i'll watch out next time, but the goto part it kinda hard to let go if you have any suggestions please, I've just stepped in c++ that why; my teacher even told me not but i can't help it may be after couple of semesters i might succeeded. and please please help me just a tip that all i need to kick in.thanks in advance.  
0 ragmar thomas · August 6, 2014
#include <iostream>
#include<fstream>
#include<string>

using namespace std;

int main()
{
   string choice, meaning, word, yesOrno;
   char add[20];

   cout << "\n\n\n\n" << endl;
   cout << "          search word -s" << endl;
   cout << "          add words -a" << endl;
   cout << "          to exit -q" << endl;

   cin >> choice;
   cin.ignore();

   while(choice!="q")
   {
      if(choice=="a")
      {
        do{cout << "you want to add the word <y-for yes> or <n-for no> " << endl;

         cin >> yesOrno;
         cin.ignore();

            ofstream file;
            file.open(add);
            
            cout << "the  word you want to add please enter" <<endl;
            cin.getline(add, 20);
            file.open(add);
            cout << "please enter the meaning of the word" << endl;
            getline(cin, meaning);
            file << meaning;

           }while(yesOrno != "y");/*here the loop whether it is '==' or
''!=' "y" it keeps it keeps doing what is in the brace*/

      }
      else if(choice=="s")
      {
         char sch[20];
         ifstream nfile(add);
         cout << "please enter the word you want to search for: ";
         cin.getline(sch, 20);

         //nfile.open(add);
         while(nfile.is_open())
         {
            if(add==sch) /*here this is supposed to do comparing word from the user and existing words 'm not sure about this part honestly*/
            {
               cout << "the meaning of you word is " << endl;
               while(nfile.is_open())
               {
                  getline(nfile, meaning);
                  cout << meaning;
               }
            }
            else if(add!=sch)
            {
               cout << "word not found" << endl;
            }
         }
      }
   }
}





thanks for the tips guys i'll try to be better next time and i have tried to replace the goto with (do{}while) but it ain't working. i'll keep trying to fix it but for the mean while this is the best that i can do thanks in advance guys,c u round.
0 ragmar thomas · August 6, 2014
1.the rather one, the word get saved as a file name
here are u suggesting to use different file name and saving the word and its meaning together?


2.just for comparing from the saved file names "words" to help the ifstream to read file from the files saved as word by that particular file name or "word" that the user want to find out or "search" or simply to read the saved data or file in that particular file name or "word".
  • 1

C++

107,111 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator