Problem in C++ about delimited strings assigned to diff arrays

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 859 Views - Last Post: 31 March 2012 - 01:41 AM Rate Topic: -----

#1 johnyianni  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 20
  • Joined: 11-February 12

Problem in C++ about delimited strings assigned to diff arrays

Posted 20 March 2012 - 07:55 PM

Hello There!

Greetings!

I am a newbie in c++.Now i have a problem.With my source code..What should i do..This is my problem...

This program tries to retrieve datas from a notepad, named employee.txt,consisting of employees data,
Such as:idnumber,employee name,salary grade.
Ex. A1001,John Davidson,1
A1002,Andrew Jackson,2

I use getline to delimit the string...

Now my problem is how am i going to assign the delimited string into different arrays??
Ex. A1001 and A1002 is a assigned to an array named idnumber.
John Davidson and Andrew Jackson is assigned to an array named emplname..

Now can you give me the solution to my problem??
Please help..

Can you give me a sample code?

#include<fstream>
#include<iostream>
#include<string>
#include<cstdlib>


using std::cout;
using namespace std;




void main()
{

	int x=0;
	string filename="employee.txt";
fstream empfile;
empfile.open(filename.c_str());


if(empfile.fail())
{
cout<<"Unable to open file: "<<filename<<endl;
exit(1);
}

while(empfile.good())
{
getline(empfile,line,',');//Problem Here...I want the delimited string to be assigned in diff. arrays..


}

}


This post has been edited by no2pencil: 20 March 2012 - 07:59 PM
Reason for edit:: Added code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Problem in C++ about delimited strings assigned to diff arrays

#2 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1371
  • View blog
  • Posts: 4,744
  • Joined: 19-February 09

Re: Problem in C++ about delimited strings assigned to diff arrays

Posted 20 March 2012 - 09:39 PM

You can have three read statements to read the three pieces of data in the line. Each call can assign to a different array.

{
  getline(empfile, idnumber[i], ',');
  getline(empfile, emplname[i], ',');
  getline(empfile, salarygrade[i]);


Was This Post Helpful? 0
  • +
  • -

#3 johnyianni  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 20
  • Joined: 11-February 12

Re: Problem in C++ about delimited strings assigned to diff arrays

Posted 21 March 2012 - 02:15 AM

ahhh..ok i will you try your suggestions..THanks
Was This Post Helpful? 0
  • +
  • -

#4 johnyianni  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 20
  • Joined: 11-February 12

Re: Problem in C++ about delimited strings assigned to diff arrays

Posted 21 March 2012 - 04:27 AM

Any Suggestions More??
Was This Post Helpful? -1
  • +
  • -

#5 jimblumberg  Icon User is online

  • member icon


Reputation: 4142
  • View blog
  • Posts: 12,892
  • Joined: 25-December 09

Re: Problem in C++ about delimited strings assigned to diff arrays

Posted 21 March 2012 - 05:54 AM

What have you tried? Show some code where you are actually trying to accomplish your objective.

Jim
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6078
  • View blog
  • Posts: 23,549
  • Joined: 23-August 08

Re: Problem in C++ about delimited strings assigned to diff arrays

Posted 21 March 2012 - 06:15 AM

void main()


This is WRONG. It is ALWAYS int main() in C++. ALWAYS!

And unless you show some actual effort, this topic will be closed.
Was This Post Helpful? 1
  • +
  • -

#7 johnyianni  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 20
  • Joined: 11-February 12

Re: Problem in C++ about delimited strings assigned to diff arrays

Posted 22 March 2012 - 02:15 AM

Thanks #define for your suggestion...It is greatly appreciated...
Was This Post Helpful? 0
  • +
  • -

#8 johnyianni  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 20
  • Joined: 11-February 12

Re: Problem in C++ about delimited strings assigned to diff arrays

Posted 22 March 2012 - 10:51 PM

Hello Fellas...I have here a problem in C++...
This code is running but still i am having trouble with replacing underscore to space..

I have here an array named empname[5]={"John_Cris_Davidson","Thomas_Earl_Thompson"}..
I used replace to change the underscore into space..But the result is not what i have expected..
The empname values are replaced with spaces instead of replacing just the underscore...



#include<stdio.h>
#include<iostream>
#include<fstream>
#include<string>
#include<cstdlib>

int displayinfo(string empid);
int emplog( string empid);

int main()
{
cout<<"Enter employee ID Number:"<<endl;//Displays a string that ask user to input data;
cin>>empid;
displayinfo(empid);
}

int displayinfo(string empid)
{
empfile.open(file.c_str());//This is where "employee.txt" is opened
string file="employee.txt";//file variable contains the name of the file
fstream empfile;
string emplname[5],idnumber[5],empid;//empid it is where you contain user input of employee id
//emplname,idnumber and salgrade are the container for the delimited string
int salgrade[5],x=0;


if(empfile.fail())//If opening file fails then this block of code is executed.
{
cout<<"Unable to open file: "<<filename<<endl;
exit(1);
}
else
{
while(empfile.good())//if empfile is good or file opening is a success
{
getline(empfile,emplname[x],',');
getline(empfile,idnumber[x],',');
getline(empfile,salgrade[x]);

replace(emplname[x].begin(),emplname[x].end)//Problem in THis area Please help me
x++;
}

for(int i=0;i<5;i++)
{
if(idnumber[i]==empid)
{
cout<<"ID Number:"<<idnumber[i]<<endl;
cout<<"Employee Name:"<<emplname[i]<<endl;
cout<<"Salary Grade:"<<salgrade[x]<<endl;
emplog(empid);
}
else
{
cout<<"Entered Employee Number does not exist(s). Please enter only employee number that is currently employed"<<endl;
exit(1);
}
}
}
}

This post has been edited by Atli: 22 March 2012 - 11:58 PM
Reason for edit:: Please use [code] tags when posting code.

Was This Post Helpful? 0
  • +
  • -

#9 jdavi134  Icon User is offline

  • D.I.C Head

Reputation: 42
  • View blog
  • Posts: 225
  • Joined: 26-October 11

Re: Problem in C++ about delimited strings assigned to diff arrays

Posted 23 March 2012 - 12:22 AM

You're using the replace function all wrong.

For a more detailed guide/tutorial on how to use it, try this link:

Replace Function

Jack
Was This Post Helpful? 0
  • +
  • -

#10 jimblumberg  Icon User is online

  • member icon


Reputation: 4142
  • View blog
  • Posts: 12,892
  • Joined: 25-December 09

Re: Problem in C++ about delimited strings assigned to diff arrays

Posted 23 March 2012 - 05:56 AM

Since you are just replacing a single character with a single character you have several options.

One option would be to iterate through the string character by character looking for the character that you want to remove. When you find that character replace it and continue to the end of the string.

The next option would be to use the std::string.find() function to search the string for the character you want to replace, and if it is in the string replace the character. Then repeat the find/replace until you fail to find any more of your characters.

Edit: Also since this is basically the same problem as your last post I am going to merge the two topics. Please don't open a new topic for what is basically the same problem.


Jim

This post has been edited by jimblumberg: 23 March 2012 - 06:04 AM

Was This Post Helpful? 0
  • +
  • -

#11 johnyianni  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 20
  • Joined: 11-February 12

Re: Problem in C++ about delimited strings assigned to diff arrays

Posted 25 March 2012 - 03:11 AM

is this correct??

while(int c=empname[a].find("_"))
{
empname[a].replace(c,1,"_");
}


:code:

This post has been edited by jimblumberg: 25 March 2012 - 07:05 AM
Reason for edit:: Added missing Code Tags, Please learn to use them.

Was This Post Helpful? 0
  • +
  • -

#12 jimblumberg  Icon User is online

  • member icon


Reputation: 4142
  • View blog
  • Posts: 12,892
  • Joined: 25-December 09

Re: Problem in C++ about delimited strings assigned to diff arrays

Posted 25 March 2012 - 07:04 AM

No that is not correct. If you insist on using find and replace you need to carefully read the documentation for these functions.

First what is the return type of find()? What does find() return on failure? It does not return an int. It returns a size_t, which is an unsigned type. This is an important difference, because find returns the value std::string::npos on failure. This value is not zero. It is a very large number, the largest number that a size_t can hold which in many cases is larger than what a signed int can hold. The only time find() returns zero is when it finds the search character in the first character in the string. So you will probably have an endless loop with your code.

Now lets move on to your replace call. First, what happens when you don't "find" your character? What is the value of c? As I stated in the previous paragraph it returns std::string::npos. So because c is equal to std::string::npos your program will throw an exception and crash, because c is larger than the size of your string. You need to insure that c is less than the size of your string before calling the replace function. Also you say you want to replace the '_' character why are you replacing the character with a '_' character? Wouldn't the space character ' ' be a better choice?


You would be better off just iterating through the string and looking at each individual character and if it is the value you are looking for replace it with your replacement variable.


When posting code use code tags! :code:


Jim

This post has been edited by jimblumberg: 25 March 2012 - 07:05 AM

Was This Post Helpful? 0
  • +
  • -

#13 johnyianni  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 20
  • Joined: 11-February 12

Re: Problem in C++ about delimited strings assigned to diff arrays

Posted 25 March 2012 - 07:48 AM

Sorry its an typographical error....yeah your right...
Was This Post Helpful? 0
  • +
  • -

#14 johnyianni  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 20
  • Joined: 11-February 12

Re: Problem in C++ about delimited strings assigned to diff arrays

Posted 30 March 2012 - 03:27 AM

I get it ...not using replace method...
but this...

int b,flag=0;
for(b=0;b<5;b++)
{

if(empidno[b]==empid)
{
for(d=0;d<empname[b].size();d++)
{
if(empname[b][d]=='_')
{
empname[b][d]=' ';
}
}
cout<<"Name: "<<empname[b]<<endl;
cout<<"Salary Grade: "<<empsalgrade[b]<<endl;
flag=1;
}
else if(flag!=1)
{
cout<<"Employee does not exist"<<endl;
}


}

Was This Post Helpful? 0
  • +
  • -

#15 jimblumberg  Icon User is online

  • member icon


Reputation: 4142
  • View blog
  • Posts: 12,892
  • Joined: 25-December 09

Re: Problem in C++ about delimited strings assigned to diff arrays

Posted 30 March 2012 - 05:48 AM

Very good. Now you can use some code formatting to make your code more readable. See this link: Indentation Styles, pick one and use it consistently.

Jim
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2