473,398 Members | 2,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Problem reading string from input file then subsequent integers

Hi this is part of a function that reads in data and stores in into vectors

the input looks like

CL905 1 2
SB11 2 3
SB12 2 7 4 5
SB13 2 6 4

now when i run this code the output i get that uses this code is
CL905 1 2
CL905 1 2
CL905 1 2


now i've checked the out put code and its find whats wrong is it keeps taking in the first line of code. Any help?

for(int i = 0; i < placesamount; i++){
fin >> service.name;
cout << " in " << service.name << endl;

while( fin >> place ){



service.AddPosition(place);

}

service.AddPosition(0);
Service.push_back(service);


}
Jul 16 '07 #1
3 1671
weaknessforcats
9,208 Expert Mod 8TB
I can't tell unless I know what the definiton service.name and place are.

Almost certainly, the >> operator encounted a byte that couldn't be part of the variable being extracted, so the error flag is set which effectively disables all further >> operations.
Jul 16 '07 #2
I can't tell unless I know what the definiton service.name and place are.

Almost certainly, the >> operator encounted a byte that couldn't be part of the variable being extracted, so the error flag is set which effectively disables all further >> operations.
Expand|Select|Wrap|Line Numbers
  1.  
  2. service is an object of this class and place is just an int varible that i'm pusing into the town vector
  3.  
  4. i populate service then i push it into another vector. 
  5. class Services{
  6. public:
  7.     Services();
  8.  
  9. vector<int> town;
  10.  
  11. string name;
  12.  
  13.  
  14.  
  15. void AddPosition(int postion);
  16.  
  17.  
  18.  
  19.  
Anyway i came up with a new solution
It's ugly but it works

Expand|Select|Wrap|Line Numbers
  1.   for(int i = 0; i < placesamount; i++){
  2.     fin >> service.name;
  3.     cout << " in " << service.name << endl;
  4.     service.town.clear();
  5.  
  6.     newcycle = true;
  7.     other = "z";
  8.  
  9.     while(fin.get(pl) && pl != '\n'){
  10.  
  11.  
  12.         if(pl != ' '){
  13.  
  14.         rest = pl;
  15.  
  16.         }    
  17.  
  18.  
  19.  
  20.  
  21.  
  22.             other =    rest;
  23.  
  24.  
  25.             if(other == repeat){
  26.             repeatcount++;
  27.             }
  28.             if(other != repeat || repeatcount > 2){
  29.             repeatcount = 0;
  30.             place = atoi(other.c_str());
  31.  
  32.             if(place > 0){
  33.             cout << place << endl;
  34.                 service.AddPosition(place);
  35.  
  36.                 repeat = other;
  37.  
  38.  
  39.             }
  40.  
  41.             }
  42.  
  43.  
  44.  
  45.  
  46.  
  47.         //service.AddPosition(place);
  48.  
  49.  
  50.     }
  51.  
  52.     service.town.push_back(0);
  53.  
  54. Service.push_back(service);
  55.  
  56. }  
Jul 16 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
You never told me tjhe type of place.

However, service.name is a string so

cin>> service.name

would fetch CL905 and then stop at the space.

That would require place to be an int.

When you get to the \n

cin>> service.name

extracts nothing.

Then you are back to cin>> place with a \n in the input buffer and that will set the fail flag and disable further cin>> operations.

Using cin.get() is is only way to work around this.
Jul 16 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: slickn_sly | last post by:
For some reason, I'm getting an error. It doesn't seem to be reading my "input.txt" file. I'm trying to read the "input.txt" file which consits of random integers and use insertion sort to sort...
7
by: Kay | last post by:
1) If i want to read data from a txt file, eg John; 23; a Mary; 16; i How can I read the above data stopping reading b4 each semi-colon and save it in three different variables ? 2) If I...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
7
by: fakeprogress | last post by:
For a homework assignment in my Data Structures/C++ class, I have to create the interface and implementation for a class called Book, create objects within the class, and process transactions that...
10
by: Tyler | last post by:
Hello All: After trying to find an open source alternative to Matlab (or IDL), I am currently getting acquainted with Python and, in particular SciPy, NumPy, and Matplotlib. While I await the...
0
by: Anish G | last post by:
Hi, I have an issue with reading CSV files. I am to reading CSV file and putting it in a Datatable in C#. I am using a regular expression to read the values. Below is the code. Now, it reads...
9
by: Eric Lilja | last post by:
Hi! I have a program with a class that needs to be able to write itself to a file in clear text format. The file has two integers and vector of struct objects. The struct has a string that can...
6
by: efrenba | last post by:
Hi, I came from delphi world and now I'm doing my first steps in C++. I'm using C++builder because its ide is like delphi although I'm trying to avoid the vcl. I need to insert new features...
1
by: Sachin Garg | last post by:
I have a program which opens a fstream in binary input+output mode, creating the file if it doesn't exists. But writing doesn't works after reading, it must be something obvious that I am not aware...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.