Problem att läsa siffror från txt-fil (c++)
Hej.
Jag har lite problem med ett projekt i C++.
Problemet är sådant att jag skulle vilja läsa ett antal siffror från en .txt-fil, vilka sedan skall jämföras med ett lösenord som användaren skriver in. Stämmer lösenordet så får användaren tillgång till programmet, stämmer det inte så får denne inte tillgång.
Observera att det här inte är ett skolprojekt, utan jag programmerar bara för att jag tycker det är kul.
Här är så långt som jag har kommit:
int funktion_pass()
{
double a;
double b;
char *filename = "banksettingsconfig.txt";
ifstream file_in(filename);
ofstream file;
if (!file_in)
{
cout << "There was a problem opening file "
<< filename
<< " for reading."
<< endl;
return 0;
}
cout << "Opened " << filename << " for reading." << endl;
while (file_in >> a)
{
if (a == 000000)
{
return 0;
}
else
{
cout << "Enter your password: ";
cin >> b;
if (a == b)
{
cout << "Password accepted\n";
cin.get();
return 0;
}
else
{
cout << "Wrong password\n";
cout << "Terminating...\n";
int funktion_exit();
return 0;
}
}
}
return 0;
}
int funktion_exit()
{
return 0;
}
Det här är en funktion som jag döpt till "Funktion_pass" och är alltså bara en liten del av hela programmet.
Problemet jag har är att även om man skriver in fel lösenord så är det bara att trycka på enter för att ändå få tillgång till programmet.
För att råda bukt på det hela så skapade jag en ny funktion, "Funktion_exit". Det enda den gör är att avsluta programmet. Trots detta så får jag det inte att fungera.
Jag har en liten aning om att det kanske kan vara raden "While (file_in >> a)" som ställer till det för mig, men jag har ingen aning om vad jag kan göra för att få bort den.
Själva kod-biten som läser från filen har jag bara kopierat från en sida (www.cplusplus.com) så egentligen har jag ingen aning om vad den gör.
Som ni kanske också ser så är jag inte en avancerad programmerare. Därför skulle det hjälpa oerhört om svaren ni ger inte heller innehåller så avancerad kod. Håll det enkelt!
Här kommer hela programmet om ni vill ha lite mer inblick i problemet:
#include <iostream>
#include <math.h>
#include <fstream>
#include <ctime>
#include <string>
using namespace std;
using std::cout;
using std::cin;
int funktion_main();
int funktion_in();
int funktion_out();
int funktion_status();
int funktion_first();
int funktion_pass();
int funktion_exit();
int funktion_main()
{
char choice;
do
{
cout << "What do you want to do?\n";
cout << "1. Insert money.\n";
cout << "2. Withdraw money.\n";
cout << "3. Check status.\n";
cout << "4. Exit.\n";
cin >> choice;
switch (choice)
{
case '1':
funktion_in();
break;
case '2':
funktion_out();
break;
case '3':
funktion_status();
break;
case '4':
return 0;
default:
cout << "Wrong number.\n";
return 0;
}
}
while (choice != '4');
return 0;
}
int funktion_in()
{
double a;
double b;
double c;
char *filename = "banksettings.txt";
ifstream file_in(filename);
ofstream file_out;
if (!file_in)
{
cout << "There was a problem opening file "
<< filename
<< " for reading."
<< endl;
return 0;
}
cout << "Opened " << filename << " for reading." << endl;
while (file_in >> a)
{
cout << "You currently have " << a << " kronor on your account.\n";
cout << "How much would you like to insert?\n";
cin >> b;
c = a + b;
cout << "You now have " << c << " Kronor on your account.\n";
file_out.open ("banksettings.txt");
file_out << c;
file_out.close();
cin.get();
cin.get();
return 0;
}
}
int funktion_out()
{
double a;
double b;
double c;
char *filename = "banksettings.txt";
ifstream file_in(filename);
ofstream file_out;
if (!file_in)
{
cout << "There was a problem opening file "
<< filename
<< " for reading."
<< endl;
return 0;
}
cout << "Opened " << filename << " for reading." << endl;
while (file_in >> a)
{
cout << "You currently have " << a << " kronor on your account.\n";
cout << "How much would you like to withdraw?\n";
cin >> b;
if (a - b < 0)
{
cout << "You don't have that amount of money on your account.\n";
cout << "You do, however, have " << a << " kronor on your account.\n";
cin.get();
cin.get();
return 0;
}
else
{
c = a - b;
cout << "You now have " << c << " Kronor on your account.\n";
file_out.open("banksettings.txt");
file_out << c;
file_out.close();
cin.get();
cin.get();
return 0;
}
}
}
int funktion_status()
{
double a;
char *filename = "banksettings.txt";
ifstream file_in(filename);
if (!file_in)
{
cout << "There was a problem opening file "
<< filename
<< " for reading."
<< endl;
return 0;
}
cout << "Opened " << filename << " for reading." << endl;
while (file_in >> a)
{
cout << "You currently have " << a << " kronor on your account\n";
}
cin.get();
cin.get();
return 0;
}
int funktion_first()
{
int array_one[6];
ofstream file;
ofstream file_2;
cout << "Preparing for first time use...\n";
file.open ("banksettings.txt");
file << "0\n";
file.close();
cout << "Do you want to choose a password to secure your account?\n";
cout << "If yes, type it below. If no, type zero six times (000000).\n";
cout << "Your password must consist of six numbers.\n";
cout << "\n";
cout << "Password: \n";
cin >> array_one[0], array_one[1], array_one[2], array_one[3], array_one[4], array_one[5];
cout << "Your password is: " << array_one[0], array_one[1], array_one[2], array_one[3], array_one[4], array_one[5];
cout << "\n";
cout << "Saving password...\n";
file_2.open ("banksettingsconfig.txt");
file_2 << array_one[0], array_one[1], array_one[2], array_one[3], array_one[4], array_one[5];
file_2.close();
cout << "Done.\n";
cin.get();
cin.get();
funktion_main();
return 0;
}
int funktion_pass()
{
double a;
double b;
char *filename = "banksettingsconfig.txt";
ifstream file_in(filename);
ofstream file;
if (!file_in)
{
cout << "There was a problem opening file "
<< filename
<< " for reading."
<< endl;
return 0;
}
cout << "Opened " << filename << " for reading." << endl;
while (file_in >> a)
{
if (a == 000000)
{
return 0;
}
else
{
cout << "Enter your password: ";
cin >> b;
if (a == b)
{
cout << "Password accepted\n";
cin.get();
return 0;
}
else
{
cout << "Wrong password\n";
cout << "Terminating...\n";
int funktion_exit();
return 0;
}
}
}
return 0;
}
int funktion_exit()
{
return 0;
}
int main()
{
int a;
cout << "IS THIS THE FIRST TIME YOU ARE USING THIS FILE?\n";
cout << "PRESS '1' IF YES AND '2' IF NO.\n";
cin >> a;
if (a == 1)
{
funktion_first();
funktion_main();
cout << "terminating...\n";
}
else
{
funktion_main();
cout << "terminating...\n";
}
return 0;
}
Tack på förhand.
/Calle