I'm trying to make a program that's just an general assistant. It works great. Just one glitch. When i type in my login username. It repeats "write something:" twice for some reason. I've come to a conclusion that it's the getline command after the cout command that triggers it. But how can i fix that?
[code]
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string>
#include <fstream>
#include <conio.h>
#include <math.h>
#include <Windows.h>
using namespace std;
string user_input;
string current_user_profile, current_user_password;
string username, password;
string newprofile;
string makenewusername, makenewpassword;
int user_birth_year;
int currentyear = 2019;
int user_age;
int current_month;
int user_birth_month;
int user_bmonth;
void TalkToAi() { //VOID TALKTOAI
while (true) {
cout << "write something: ";
getline(cin, user_input);
transform(user_input.begin(), user_input.end(), user_input.begin(), ::tolower); //TRANSLATES ALL UPPERCASE LETTERS TO LOWER CASE SO THE SYSTEM CAN UNDERSTAND!
//IF LIBRARY!!!
if (user_input == "what's my age?" || user_input == "count my age" || user_input == "whats my age?" || user_input == "how old am i?") {
cout << "when were you born?\n" << "born: ";
cin >> user_birth_year;
user_age = currentyear - user_birth_year;
cout << "what month is it? (in numbers)\n" << "month: ";
cin >> current_month;
cout << "\n" << "what month were you born? (in numbers)\n" << "month:";
cin >> user_birth_month;
if (current_month <= user_birth_month) {
user_age = user_age - 1;
}
user_bmonth = current_month - user_birth_month;
if (user_birth_month == 12) {
user_bmonth = current_month;
}
cout << "you are " << user_age << " years old and " << user_bmonth << "months old\n";
}
}
}
void StartUp() { //VOID UPONSTARTUP (WHAT TO DO, ALSO READS DIFFRENT PROFILES AND PASSWORDS FOR LOGIN)
cout << "what profile should i load?" << "\n" << "profile name: ";
cin >> current_user_profile;
fstream Myfile; //OPEN FILE TO STORE USERNAMES
Myfile.open("Usernames_and_passwords.txt", fstream::in | fstream::out | fstream::app);
if (Myfile.is_open()) {
while (getline (Myfile, username) ) { //CHECKS IF USERNAME EXISTS
if (username == current_user_profile && password == current_user_password) {
cout << "\n" << "Hello, " << username << "\n";
return;
}
}
cout << "wrong username or username unfortunately not found.\n"; //IF IT DOESN'T, CREATE NEW USERNAME
cout << "shall i create a new profile? Yes/No: ";
cin >> newprofile;
if (newprofile == "Yes" || newprofile == "yes") {
cout << "new profile username: ";
cin >> makenewusername;
Myfile.clear();
Myfile.seekg(0, ios::beg);
Myfile << makenewusername << endl;
if (newprofile == "no" || newprofile == "No") {
return;
}