I have a small problem, and I would really appreciate any help!
What I am trying to do is a small program that creates accounts (User name, age,...), and stores it on a CSV file. And later I can search for a user and edit some information about him (example, his age).
#include <fstream>
#include <iostream>
#include <string>
#include <stdio.h>
#include <io.h>
#include <stdlib.h>
#include <string>
#include <cmath>
#include <cstdlib>
usingnamespace std;
void insert();
void searchaccount();
void testsearch();
void searchaccountold();
int main()
{
// A SMALL MENU!
system ("title Test App");
system ("cls");
system ("color 0F");
printf(">> Welcome to the new Test App << \n");
printf(" \n");
int choice;
printf("1) Enter 1 to create a new account \n");
printf("2) Enter 2 to see ALL the accounts \n");
printf("3) Test of the search option with user name \n");
printf("4) Enter 4 to use the old TXT search \n");
printf(" \n");
printf(" \n");
cin >> choice;
switch (choice){
case 1:
system ("cls");
insert();
break;
case 2:
searchaccount();
break;
case 3:
testsearch();
break;
case 4:
searchaccountold();
break;
cin.get();
}
}
//THIS WILL CREATE A NEW ACCOUNT (And also create the CSV file if it doesnt exist)
void insert()
{
FILE *fp;
char s[80];
char name[80];
int t;
int age;
if((fp=fopen("test.csv", "a+")) == NULL) {
printf("Cannot open file.\n");
exit(1);
}
printf("Enter The name of the client: ");
fscanf(stdin, "%s", name); /* cin */
fprintf(fp, "%s ;", name); /* puts this in the file*/
printf("Enter The age of the client: ");
fscanf(stdin, "%d", &age);
fprintf(fp, "%d\n", age);
fclose(fp);
main();
}
//------------------------
//THIS WILL SHOW ALL THE INFO(text, numbers, etc) THAT IS IN THE FILE
void searchaccount(){
string line;
ifstream file ("test.csv");
if (file.is_open())
{
while ( file.good() )
{
getline (file,line);
cout << line << endl;
}
system ("pause");
cin.get();
file.close();
}
else cout << "Unable to open file";
}
//----------------------------------
//THIS IS A SMALL TEST USING THE CSV FILE TO SEARCH FOR A SPESIFIC USER
void testsearch(){
string line;
string str;
string name;
int age;
ifstream myfile ("test.csv");
cout << "Enter the users name:";
cin >> str;
if (myfile.is_open())
{
cout << "Entered the IF ";
while ( myfile.good() )
{
cout << "Entered the WHILE";
getline (myfile,line);
while (myfile >> name >> age)
{
if (str == name){
cout << line << endl;
}
}
}
}
system ("pause");
cin.get();
myfile.close();
}
//-----------------------------
//THIS IS AN OLD PART OF CODE FROM MY PREVIOUS ATTEMPT
//USING A .TXT FILE. HERE I AM ABLE TO ACTUALY FIND THE
//USER AND DISPLAY IT ON THE SCREEN
void searchaccountold()
{
ifstream myfile("test.csv");
string name;
string str;
int age;
int money;
system("CLS");
cout << "Enter the user name:";
cin >> str; //This will have the name of the user I want to find
while (myfile >> name >> age >> money)
{
cout << "you entered the while\n" << endl;
if (str == name){
system("CLS");
cout << "User found!" << endl;
cout << "Name" << " " << "Age" << " " << "Money" << endl;
cout << "---------------------" << endl;
cout << name << " " << age << " " << "$" << money << endl;
system ("pause");
cin.get();
main();
}
if(!(str == name))
{
system("cls");
cout << "No user found!" << endl;
}
}
system ("pause");
cin.get();
main();
}
Looking at the "old code" ("void searchaccountold()"), I am sure the problem is on the "while" part ("while (myfile >> name >> age >> money)").
It should work, but it doesnt. I am almost sure the problem lies on the ";"... Because the CSV uses cells, and to pass to the second cell I would normaly need an ";" and in my case I have ">>". I have tryed to change them, to use getline, etc... and didnt work :(
I tryed what you told me, and it "kinda" worked... I am sure I am doing something totally stupid... here is what I am geting now:
When I have while (getline(myfile, name, ';')) the program works, but it only gives me back the first person on my CSV file. Example, if I search for "Tom" it will give me "Tom" and then random numbers(since I dint declared "age" and "money" on the "while"). If I search for "Bob", it says he dint found anyone with that name.
When I use this code while (getline(myfile, name, age, money, ';')) , it says this on the compiler "no matching function for call to `getline(std::ifstream&, std::string&, int&, int&, char)'". I even tryed doing with this while (getline(myfile, name,';', age,';', money, ';')), and nothing. I know I am doing something really stupid, but I cant find the solution. Can you give me another hint please :) ?
This is what my CSV file looks like(";" means going to a different cell):
TOM ; 25 ; 5000
Bob ; 31 ; 7500
Anna ; 63 ; 1200
don't use ';' for the line. What I thought of was this:
1 2 3 4 5 6 7
std::string line;
while (std::getline(myfile, line))
{
std::stringstream ss(line); // feed the stringstream with the line
if(getline(ss, name, ';') && getline(ss, age, ';') && ...) // retrieve the data from that line
...
cout << "you entered the while\n" << endl;
it says this on the compiler "no matching function for call to `getline(std::ifstream&, std::string&, int&, int&, char)'"
This is because getline is either istream& getline (istream& is, string& str, char delim);
or istream& getline (istream& is, string& str);
it extracts from is and stores in str until the delimiter is reached, or in the case of the second one until the newline character '\n' is reached. Basically the compiler is complaining because getline wasn't designed to take the input parameters that you supplied it. Follow coder777's advice and have your while take std::getline(myfile, line). That way, it will execute as long as there is another line to get, and then deal with what's actually in that line in the body of the while loop.