3 Replies - 312 Views - Last Post: 05 January 2013 - 10:45 AM Rate Topic: -----

#1 DTSDev  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 04-January 13

"Database" insert help

Posted 04 January 2013 - 06:24 PM

Hi everybody! This is my first time posting in this forum and have read the rules (plus ive been on other forums so im aware of general standards) but if im breaking one please let me know because its unintentional. so heres my problem. im learning elvish but there are a lot of words so i decided to make a program where i can insert words in both english and elvish and then when i need to look up a word i can type it in and find the corresponding word in the other language. i put database in quotes because its in corresponding text files because it was taking too long to install mysql then learn how to interface with it in c++. heres my code:
#include <iostream>
#include <map>
#include <string>
#include <cstdlib>
#include <fstream>

using namespace std;

typedef map<string, string> table;

void addWords();
void findWords();
void listWords();
void help();

int main()
{
    string input;
    
    while(true)
    {
        cout<<"Action: ";
        getline(cin, input);
        
        if(input == "add words")
            addWords();
            
        else if(input == "find specific words")
            findWords();
            
        else if(input == "see all words")
            listWords();
            
        else if(input == "help")
            help();
            
        else if(input == "exit")
            exit(1);

        else
            cout<<"error: invalid action. type help for help"<< endl;
    }
}

void addWords()
{
    string input;
    char answer;
    fstream file;
    
    do
    {
        cout<<"elvish word: ";
        getline(cin, input);
        
        cout<<"So, is \""<< input <<"\" correct (y/n)? ";
        cin>> answer;
        
        if(answer == 'y')
            break;
        
        else if(answer == 'n')
            continue;
        
        else
            cout<<"error: invalid input option"<< endl;
    }while(true);
    
    file.open("/Users/Shared/elfWordList.txt");
    file<< input << endl;
    file.close();
    
    do
    {
        cout<<"common word: ";
        getline(cin, input);
        
        cout<<"So, is \""<< input <<"\" correct (y/n)? ";
        cin>> answer;
        
        if(answer == 'y')
            break;
        
        else if(answer == 'n')
            continue;
        
        else
            cout<<"error: invalid input option"<< endl;
    }while(true);
    
    file.open("/Users/Shared/commonWordList.txt");
    file<< input << endl;
    file.close();
}

void findWords()
{
}

void listWords()
{
}



here is what happens when i run it:
Action: add words
elvish word: mellon
So, is "mellon" correct (y/n)? y
common word: So, is "" correct (y/n)? 



it skips input on the common word
also im using xcode

Is This A Good Question/Topic? 0
  • +

Replies To: "Database" insert help

#2 #define  Icon User is online

  • Duke of Err
  • member icon

Reputation: 1346
  • View blog
  • Posts: 4,637
  • Joined: 19-February 09

Re: "Database" insert help

Posted 04 January 2013 - 07:18 PM

cin >> leaves a newline character in the input stream.

057	        cin >> answer;
                // ...
079	        cin >> answer;



the next getline will read the newline and treat it as an empty string.

One option is to use cin.ignore to remove the newline character.
Was This Post Helpful? 0
  • +
  • -

#3 DTSDev  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 04-January 13

Re: "Database" insert help

Posted 04 January 2013 - 10:39 PM

ok so i did what u said and that worked, but i revised my code to have a setup, and now its not creating/writing to the files i specify
#include <iostream>
#include <fstream>
#include <sys/stat.h>
#include <sys/types.h>
#include <string>
#include <cstdlib>

using namespace std;

void addWords();
void checkSetup();

int main(int argc, char* argv[])
{
    string input;
    
    while(true)
    {
        cout<<"Action: ";
        getline(cin, input);
        
        if(input == "add words")
            addWords();
        
        else if(input == "exit")
            exit(1);
        
        else
            cout<<"error: invalid option. type \"help\" for help"<< endl;
    }
}

void addWords()
{
    string commonWord, elvishWord;
    char choice;
    ofstream file;
    
    checkSetup();
    
    while(true)
    {
        while(true)
        {
            cout<<"common word: ";
            getline(cin, commonWord);
            
            cout<<"So, is \""<< commonWord <<"\" correct (y/n)? ";
            cin>> choice;
            cin.ignore();
            
            if(choice == 'y')
            {
                file.open("/Application/elfTranslator/commonWordList.txt");
                file<< commonWord << endl;
                file.close();
                break;
            }
            
            else if(choice == 'n')
                continue;
            
            else
                cout<<"error: invalid input"<< endl;
        }
        
        while(true)
        {
            cout<<"elvish word: ";
            getline(cin, elvishWord);
            
            cout<<"So, is \""<< elvishWord <<"\" correct (y/n)? ";
            cin>> choice;
            cin.ignore();
            
            if(choice == 'y')
            {
                file.open("/Application/elfTranslator/elfWordList.txt");
                file<< elvishWord << endl;
                file.close();
                break;
            }
            
            else if(choice == 'n')
                continue;
            
            else
                cout<<"error: invalid input"<< endl;
        }
        
        while(true)
        {
            cout<<"Do you want to add another word (y/n)? ";
            cin>> choice;
            cin.ignore();
            
            if(choice == 'y')
                break;
            
            else if(choice == 'n')
                return;
            
            else
                cout<<"error: invalid input"<< endl;
        }
    }
}

void checkSetup()
{
    ifstream fileTest;
    ofstream fileCreate;
    int dirTest;
    mode_t mode;
    
    mode = 10;
    
    dirTest = mkdir("/Applications/elfTranslator", mode);
    
    fileTest.open("/Application/elfTranslator/elfWordList.txt");
    
    if(fileTest.fail())
    {
        fileTest.close();
        fileCreate.open("/Application/elfTranslator/elfWordList.txt");
        fileCreate.close();
    }
    
    fileTest.open("/Application/elfTranslator/commonWordList.txt");
    
    if(fileTest.fail())
    {
        fileTest.close();
        fileCreate.open("/Application/elfTranslator/commonWordList.txt");
        fileCreate.close();
    }
}


Was This Post Helpful? 0
  • +
  • -

#4 #define  Icon User is online

  • Duke of Err
  • member icon

Reputation: 1346
  • View blog
  • Posts: 4,637
  • Joined: 19-February 09

Re: "Database" insert help

Posted 05 January 2013 - 10:45 AM

I think you should use a container such as map. Then allow the user to load the data from the file and save to the file. It is simpler than what you are doing and lots of programs use this document type of model.

Then your add word function would only need to add to a container.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1