Problem in files

Hi Guys! hope you will be fine... I have a little bit problem in my program, I made a file to store my program,

I declare 3 variables int idNumber; string name; double money;

Problem: When I run this Program and give name like Junaid so it works perfect and goes further, but when I gives Junaid Khan then its terminates and doesn,t go further, I want to give full name which it doesn,t take??? can anybody please give me idea and sort it out please...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  #include <iostream>
#include <fstream>

using namespace std;

int main()
{
    ofstream file("khan1.txt");

    cout<<"Enters Player ID, Player Name, & Money " << endl;
    cout<<"Press CTRL+X to Quit \n" << endl;

    int idNumber;
    string name;
    double money;

    while(cin >> idNumber >> name >> money ){
        file << idNumber << ' ' << name << ' ' << money << endl;

    }
    return 0;
}
the space gives the indication of two parameters if you give the full name. This makes the program see name as the first word, and money as the second which causes the input stream to error out.

This is only because of how you ask for the inputs in the code you present. If you were to break up the idea as three separate inputs rather one input you would alleviate the issue.
so how to print the full name? I want in the following manner

1 Junaid khan 456.7


1 = idNumber
Junaid Khan = name
456.7 = money

Please help me out,,, I didn,t get you....
basically,
1
2
3
4
5
6
7
8
9
// input looks like this and not what you have.
// this will be three separate inputs or I would input the two numbers first and the name last.

cin >> Idnumber;
cin >> name;  // this will capture the full name in one input.
cin >> money;

// this will output the information.
cout << Idnumber << name << money << endl;
brother I agree but if you look over my program source code so I am taking input from user inside a While Loop, so how you can insert cin separately in while loop,,
The program is actually all about a file, I am making a file, just write the above code and save it and then go to that location where you saved it, you will see you will have a text file there , and when you will run the program and whatever you will write in the command screen, it will be saved in that text file,
I declare 3 variables, and then using these 3 variables to ask the user to give input to those using while loop, while loop is here because it will take input from user, until user press CTRL+z from keyboard which will terminate the program,,
so guide me here that using how to use your above code inside while loop...?? Please
i am trying to understand why you chose control-x to quit. I would key off the id, which in most cases would be valid or 0. If it was zero wouldn't I quit, because I had no more data?

1
2
3
4
5
6
7
8
// basically I will read in the Id number and test it for zero.
while(cin >> IdNumber != 0)
{
    cin >> name;
    cin >> money;
 
    file << IdNumber << name << money;
}


Hope that helps.
Slight modification to Azagaros' prior code:
1
2
3
cin >> Idnumber;
getline(cin, name);
cin >> money;


cin >> foo reads up to a space or a newline. The getline function reads up to a newline or a delimiter you set.

Edit:
Typos
Last edited on
Thanks to all my brothers!! Well I modified my source code according to your precious suggestions and code that you people provided,, but still the same problem.. I want to print the full name but I want to have space in between my Name e.g( Junaid Khan) not want to print (JunaidKhan)..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    ofstream file("khan1.txt");

    cout<<"Enters Player ID, Player Name, & Money " << endl;


    int idNumber;
    string name;
    double money;

    while( cin>> idNumber != 0 ){
        cin>>idNumber;
        getline(cin, name);
        cin>>money;

        file << idNumber << name << money << endl;

    }
    return 0;
}
Remove line 18. You are streaming input to idNumber twice. Look at Azagaros' example more closely.
Personally for line 17, I would check if the stream state is good with this while(cin >> idNumber) because I have no idea if Azagaros' version is doing the same thing or has some other intention.
I caught the problem: its all about to give space in between first and last name if you write JunaidKhan so works OK but if you write Junaid Khan then problem
Problem is the following:

try the source code....
when you will run the program and will give follwing input:

1 JunaidKhan 45.6 (will work) Program goes further

1 Junaid khan 45.6 (won,t work) program terminates

1 junaid khan 45 ((won,t work) program terminates)


so what to do with it I think string takes place of Double integer which is declared Money in this case.
Program doesn't terminate for me. All I found was that I had to press enter before entering in the money.

By the way, why do you have to store this information in separate variables if you're going to clump them together into the same file immediately? Have you considered just streaming the input to a single string and having the user type a special character? For example, have the user type in the format: "ID | Name | Money," so you can process the input easier.
Hey Daleth just process them in a single line but give the following input then see and try to solve it out please..

1 JunaidKhan 45.6 (will work) Program goes further

1 Junaid khan 45.6 (won,t work) program terminates

1 junaid khan 45 ((won,t work) program terminates)
The original program (very first post) has this line 18:
 
    file << idNumber << ' ' << name << ' ' << money << endl;


But the latest version above has thus line 22:
 
    file << idNumber << name << money << endl;

Therefore, regardless of the input, the output is handled in a different way.

As for the input, it depends how you are entering it at the keyboard. That is, do you press [enter] after entering the name, or not until after typing the money amount?
Last edited on
A possible version. Here I have taken the input from a file rather than the keyboard, as it makes testing more consistent.
This is my input file, "test.txt"
1
JunaidKhan
45.6
2
Junaid khan
45.6
3
junaid khan
45
0

And this is the output:

1	JunaidKhan	45.6
2	Junaid khan	45.6
3	junaid khan	45


Source code - note I have separated the output fields with the tab character '\t' as this makes it easy to distinguish the end of the name.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
    ifstream fin("test.txt");
    ofstream file("khan1.txt");

    cout<<"Enters Player ID, Player Name, & Money " << endl;

    int idNumber;
    string name;
    double money;

    const char spacer = '\t';

    while ( (fin >> idNumber) && (idNumber != 0 ))
    {
        fin.ignore(1000, '\n');
        getline(fin, name);
        fin>>money;

        file << idNumber << spacer << name << spacer << money << endl;
    }
    return 0;
}


------------------------------------

And, for good measure, let's read back the contents of the file just created, and display it on the screen:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
    ifstream file("khan1.txt");

    cout << "List contents of file" << endl;

    int idNumber;
    string name;
    double money;

    const char spacer = '\t';

    while (file >> idNumber)
    {
        file.ignore();
        getline(file, name, spacer);
        file>>money;

        cout << idNumber << ',' << name << ',' << money << endl;
    }

    return 0;
}
Last edited on
hey chervil

First run the program by using the following source code, when you run the program then give this input in one line 1 junaid khan 45.6 (it wont work) and press enter program terminates but if you will give this input 1 junaidkhan 45.6 it will work and at last try this 1 junaid khan 4 (it will work) Problem :
we want to print all things in one line, so if we give space in between name like Junaid Khan so then Money variable which is Double if we will give 45.6 so it wont work, and if we write name like JunaidKhan so then Double integer 45.6 works, just try out you will feel the difference.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    ofstream file("khan1.txt");

    cout<<"Enters Player ID, Player Name, & Money " << endl;


    int idNumber;
    string name;
    double money;

    while (cin >> idNumber && getline(cin, name) && cin >> money) {
    file << idNumber << name << money << endl;
}
    return 0;
}
If you enter the data all on a single line, like this:
1 junaid khan 45.6

what happens is this:
cin >> idNumber idNumber = 1
getline(cin, name) name = " junaid khan 45.6"
cin >> money - will wait for next line of data and everything gets out of sync, it won't work.

You need to either press enter after the name, before supplying the value of money, or alternatively, use a different separator such as tab in order to distinguish between the end of the name and the start of the money - as in my code example 2 above.

Another way would be to not use the formatted input such as cin >> idNumber and cin >> money at all, but instead, read the entire line and parse it for the first space and last space which will mark the three separate portions of the string. But that would require a bit more work, as you then have to convert the strings into numbers (perhaps using stringstream).

It's not really hard, it's more a design decision, decide whether to type the input at the keyboard in a way which is very easy for the program to handle, (for example using newline or tab or comma separators) or alternatively, get the program to do more work to analyse and parse the input.

Last edited on
Topic archived. No new replies allowed.