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...
// 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
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)..
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.
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.
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
And this is the output:
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.
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.