2017-01-13 6 views
1

私は、ユーザーが名前を入力できるようにするこのプログラムを作成しています。これは、しかし、動作しません。それは名前だけを取り、何もしません!最初のif文も実行されます。ユーザー作成プログラムのエラー

#include <iostream> 
#include <vector> 
#include <string> 
#include <fstream> 
#include <Windows.h> 

using namespace std; 

class user{ 
public: 
    string name; 
    int userID = rand() % 1001; 

    void setName(string n) { name = n; }; 
    string getName() { return name; }; 
    int returnID() { return userID; }; 
}; 

int main() { 
    HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE); 
    string udef; 
    user mat; 
    int id = mat.returnID(); 

     while (udef != "exit" || "Exit") { 
      SetConsoleTextAttribute(h, FOREGROUND_BLUE | FOREGROUND_GREEN |  FOREGROUND_INTENSITY); 
      cout << "Type a command to continue. You may also type help for a list of commands." << endl << "User: "; 
      getline(cin, udef); 
      if (udef == "new" || "New") { 
       cout << "Type your name:" << endl << "User: "; 
       getline(cin, udef); 
       mat.setName(udef); 
       ofstream userfile(id + ".txt"); 
       if (userfile.is_open()) { 
        cout << "Generating User info file..." << endl; 
        Sleep(10); 
        cout << "File Name : '" << id << "'" << endl; 
        userfile << mat.getName(); 
        Sleep(10); 
        cout << "Appending information..." << endl; 
        cout << "Done! " << mat.getName() << " has been written to line(s) 1 of " << id << ".txt!" << endl; 
       } 
       userfile.close(); 
      } 
      else if (udef == "help" || "Help") { 

      } 
     } 

    return 0; 
} 

答えて

1

while ((udef != "exit") && (udef != "Exit")) 

代わりの

while (udef != "exit" || "Exit") 

で試してみてください無地でシンプルな"Exit"は今までtrueあるとudefExitexitから、これまでとは異なります。

P.s .: obviuosly、同じ問題(及び同様の補正)

if (udef == "new" || "New") 

else if (udef == "help" || "Help") 

と彼らは

if ((udef == "new") || (udef == "New")) 

else if ((udef == "help") || (udef == "Help")) 
なると
+0

または[このページのtoupper()メソッド](https://stackoverflow.com/questions/735204/convert-a-string-in-c-to-upper-case)のいずれかを使用して、たとえば、 'while(boost :: to_upper(udef)!=" EXIT ")' –

+1

@ KenY-N - 合理的です。しかし、私はエラーがどこにあるのかを説明しようとしています。 – max66

+0

もちろん、OPの開発のこの時点では、誤解を訂正する方がおそらくもっと有用です。 –

関連する問題