So new here to daniweb and just finished my first semester of programming 1 and I'm trying to do some extra curricular work to get me prepared for my spring semester. Would appreciate any and all help or suggestions anyone could give me. Here's the issue:


So i was trying to make a program to let me read from 2 separate files, one being .csv and one being .dat and then taking information from both and combining them into an xml outfile. Here is the issue, the first half of my code works fine from what i can see, but the second half where i have to read from a file formatted like this:

Bright, Rich, PC12K2RT, 10/21/2011

and No matter what i change my values to it keeps giving me the same results when the program executes and looks like this in my output:

PC12K2RTUsed Car 02350.00
PC12K2RT
2
Used Car
02350.00

Bright, Rich, PC12K2RT, 10/21/2011
Bright
Bright, Rich, PC12K2RT, 10/21/2011
Bright
Bright, Rich, PC12K2RT, 10/21/2011
Bright
Bright, Rich, PC12K2RT, 10/21/2011
Bright
Bright, Rich, PC12K2RT, 10/21/2011

the first grouped section of the above input is working correctly. The second part is not chopping out each section and storing the strings separately


My intentions are to first take everything from the file into a variable, then save everything from Start to the first comma minus the comma to a second variable, and so on so forth till EOF where each string or value between commas is saved as a separate variable. Not looking to have the code just given to me but if you can show an example and explain why you did what you did i would greatly appreciate it. Been trying to mess with this on and off for almost 3 weeks now and figured someones outside opinion could be beneficial:

notes: the way i have it set up currently displays in the output prompt what will be written to the file so i know if its working or not without having to continually open the output file (the xml section is incomplete because i do not need help with that section i'm just waiting until i figure out the delimited csv portion)

my code:

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
 
using namespace std;
 
void pause()
{   
	cout << "\n Press enter to continue..."; //pause
	char junk;
	cin.ignore();
	cin.get(junk);
}
 

int main()
{
	// declare constants
 

	// declare variables
	ifstream myInfile1;
	ifstream myInfile2;
	ofstream myOutfile;
	
	string Items, partNumber, partColor, partDescript, price;
	
	string Customer, lastName, firstName, date; 
 

	// get input
	
	//myInfile1.open("c:\\c++\\Customer.csv"); //openfiles  MAKE SURE FILE NAMES AND DIRECTORIES MATCH!!!
	//myInfile2.open("c:\\c++\\Items.dat");
	//myOutfile.open("c:\\c++\\Transaction.xml"); 
 
 
	myInfile1.open("c:\\c++\\Items.dat");
	getline(myInfile1, Items);
	cout << "\n\n" << Items;
 
	partNumber = Items.substr (0,8);
	cout << "\n\n" << partNumber;
 
	partColor = Items.substr (5,1);
	cout << "\n\n" << partColor;
 
	partDescript = Items.substr (8,14);
	cout << "\n\n" << partDescript;
 
	price = Items.substr (23,8);
	cout << "\n\n" << price <<endl;
 
	myInfile1.close();
 
	
	myInfile2.open("c:\\c++\\Customer.csv");
	getline(myInfile2, Customer);
	cout << "\n\n" << Customer;
 

	//delimCount = Customer.find(",");
	//stringName = string.substring (1,intNUmber);
 

	int currentcount;
 
    currentcount = Customer.find(",");
	
	lastName = Customer.substr(0,currentcount);
    Customer.substr(currentcount,Customer.length()-currentcount+1);
	
	cout << "\n\n" << lastName;
 
	cout << "\n\n" << Customer;
 
	
	firstName = Customer.substr(0,currentcount);
 
	cout << "\n\n" << firstName;
 
	Customer.substr(currentcount,Customer.length()-currentcount+2);
		cout << "\n\n" << Customer;
 
	partNumber = Customer.substr(0,currentcount);
 
	cout << "\n\n" << partNumber;
 
	Customer.substr(currentcount,Customer.length()-currentcount+3);
		cout << "\n\n" << Customer;
 
	date = Customer.substr(0,currentcount);
 
	cout << "\n\n" << date;
 
	Customer.substr(currentcount,Customer.length()-currentcount+4);
		cout << "\n\n" << Customer;
 
		myInfile2.close();
 

   //output everything to xml
 

 

 
	// save results to outfile
 

 

 
	pause();
	return 0;
}
</fstream></string></iomanip></iostream>

p.s. i know the xml section is empty, i havent bothered starting with it because i want to figure out the delimited section first.... string.find(",") and substring were mentioned in class last semester but he never used them or went into detail about them, so thats the way i'm trying to solve it so i can learn it... he left so much crap out....

Recommended Answers

All 11 Replies

What are you attempting to do with Customer.substr(currentcount,Customer.length()-currentcount+1); ?
It looks like you get the substring but you don't do anything with it.
Is there an = missing?

What are you attempting to do with Customer.substr(currentcount,Customer.length()-currentcount+1); ?
It looks like you get the substring but you don't do anything with it.
Is there an = missing?

I posted a in depth response to this from my phone which said it went through... so here's another try.

The Customer.substr portion of code i was taught to use is suppose to take whatever is saved in "Customer" variable (in this case it starts with "Bright, Rich, PC12K2RT, 10/21/2011") It is suppose to grab the first string including the first comma and delete that from "Customer" leaving "Customer" as " Rich, PC12K2RT, 10/21/2011" with the leading whitespace.

the customer.length portion is suppose to subtract the comma or the whitespace (i dont remember which my brain hurts) and set the read point at the "R" in rich.

This this has driven me nuts... i don't even know if I'm anywhere close to the right track. All i know is using substrings i want to take the full length of the string and slowly chop it up saving each portion as a seperate variable. (i.e. lastname, firstname, partnumber, and date)

can someone point me in the right direction... im so lost its ridiculous... i already looked through string.find and other sites regarding string functions and i still don't understand...

Also i have no clue if an = is missing... i don't remember using one in school.. but i could be mistaken... i was pulling 18 hours of classes a week it may have leaked out of my head at some point

I tried to. My question was meant to point you to a line of code that looks incomplete, to point you to what I think is wrong. Not for you to post silliness like "my brain hurts" and "i have no clue if an = is missing... i don't remember using one in school". If you've gotten this far in the language we know for a fact you've used an = at some time.

Now look at and analyze what you wrote, and what you're attempting to do. Look for errors in the line I posted that does not accomplish what you need.

I tried to. My question was meant to point you to a line of code that looks incomplete, to point you to what I think is wrong. Not for you to post silliness like "my brain hurts" and "i have no clue if an = is missing... i don't remember using one in school". If you've gotten this far in the language we know for a fact you've used an = at some time.

Now look at and analyze what you wrote, and what you're attempting to do. Look for errors in the line I posted that does not accomplish what you need.

Put bluntly in the code i was using our teacher mentioned this snippet in literally 5 minutes and never covered it again. I'm trying to create code from a self taught method based off of something I have constructed out of multiple threads and forums I have seen online. I have no clue what I'm doing wrong that's why i asked for help. I don't know how the function i am trying to use completely works.

"...in the code i was using our teacher mentioned this snippet" implies you are in a class, presumably in school. Which means, usually, there is a textbook. Textbooks usually explain functions and methods being taught.

What does your textbook say about string methods, specifically substr() . Does simply calling substr() change the string? That looks like your assumption.

The rest of your post seems to imply you would rather spend hours looking at forums and web-based information rather than spend a few minutes looking at your book. Is that what's going on? In my experience, the book will describe what you want and give workable examples.

Especially when you say "I don't know how the function i am trying to use completely works." So look it up, and not on forums. Try your index. Google the function itself. Read sites that explain C++, not help sites, for (mostly) accurate information.


In looking at your code, rewriting this segment:

currentcount = Customer.find(",");
firstName = Customer.substr(0,currentcount);
cout << "\n\n" << firstName;
Customer.substr(currentcount,Customer.length()-currentcount+2);
cout << "\n\n" << Customer;

using simple integers rather than string(substr) you have:

currentcount = 200;
firstName = customer - currentcount;
cout << "\n\n" << firstName;
customer - currentcount;
cout << "\n\n" << Customer;

So what's wrong with this simpler code? What happens to customer?

"...in the code i was using our teacher mentioned this snippet" implies you are in a class, presumably in school. Which means, usually, there is a textbook. Textbooks usually explain functions and methods being taught.

What does your textbook say about string methods, specifically substr() . Does simply calling substr() change the string? That looks like your assumption.

The rest of your post seems to imply you would rather spend hours looking at forums and web-based information rather than spend a few minutes looking at your book. Is that what's going on? In my experience, the book will describe what you want and give workable examples.

Especially when you say "I don't know how the function i am trying to use completely works." So look it up, and not on forums. Try your index. Google the function itself. Read sites that explain C++, not help sites, for (mostly) accurate information.


In looking at your code, rewriting this segment:

currentcount = Customer.find(",");
firstName = Customer.substr(0,currentcount);
cout << "\n\n" << firstName;
Customer.substr(currentcount,Customer.length()-currentcount+2);
cout << "\n\n" << Customer;

using simple integers rather than string(substr) you have:

currentcount = 200;
firstName = customer - currentcount;
cout << "\n\n" << firstName;
customer - currentcount;
cout << "\n\n" << Customer;

So what's wrong with this simpler code? What happens to customer?

I do in fact have a book from last semester but the reason i began looking online was because after reading the information I had available I still didn't understand. The examples in my book don't explain anywhere what I am trying to do.

I tried the simpler code you suggested and got an operand error for the "-" operator. I tried to do a pass by value and screwed that up too so I'm still where i was before.

I may be repeating myself with this but what I THOUGHT

Customer.substr(currentcount,Customer.length()-currentcount+1);

meant was that after grabbing the string Customer:

Customer.length()-currentcount+1);

would take string Customer: "Bright, Rich, PC12K2RT, 10/21/2011" and remove the first value including the first comma and the next whitespace leaving "Rich, PC12K2RT, 10/21/2011"".

Then rinse and repeat until EOF

From everything I have read in my book, and then online, this is what I interpreted it as meaning from the code i wrote. If that's the hard way, the wrong way, I apologize. The reason I asked for help was because after reading the resources i could find available I still don't understand why it is wrong, not working, or why i shouldn't be using that specific code.

> My intentions are to first take everything from the file into a variable,
> then save everything from Start to the first comma minus the comma to a second variable,
> and so on so forth till EOF where each string or value between commas is saved as a separate variable

To split a std::string into separate tokens using a single delimiter, std::getline() and std::istringstream turn out to be quite handy.

std::vector< std::string > split( const std::string& str, char delimiter = ',' )
{
    std::vector< std::string > result ;
    std::istringstream stm(str) ;
    std::string token ;
    while( std::getline( stm, token, delimiter ) ) result.push_back(token) ;
    return result ;
}
commented: I have no rep power so here is a +zero thank you! +0

I may be repeating myself with this but what I THOUGHT

Customer.substr(currentcount,Customer.length()-currentcount+1);

meant was that after grabbing the string Customer:

Customer.length()-currentcount+1);

would take string Customer: "Bright, Rich, PC12K2RT, 10/21/2011" and remove the first value including the first comma and the next whitespace leaving "Rich, PC12K2RT, 10/21/2011"".

Then rinse and repeat until EOF

You are correct in assuming that it removes the first value, but you don't do anything with that changed string, therefore, you lost it. Just like the statement
customer - currentcount; correctly subtracts currentcount from customer, but the result is lost, since it isn't stored anywhere. You simply need to store the result:
whatever = Customer.substr(currentcount,Customer.length()-currentcount+1);

commented: put up with me for a long time... i appreciate it more then you know +0

You are correct in assuming that it removes the first value, but you don't do anything with that changed string, therefore, you lost it. Just like the statement
customer - currentcount; correctly subtracts currentcount from customer, but the result is lost, since it isn't stored anywhere. You simply need to store the result:
whatever = Customer.substr(currentcount,Customer.length()-currentcount+1);

Aha ok now everything makes sense now. Thats what you meant originally by = is missinf somewhere. Thank you! Gonna work on it now.

Yep. Just trying to make you think...

Aha ok now everything makes sense now. Thats what you meant originally by = is missinf somewhere. Thank you! Gonna work on it now.

Here is the finished code for reference of anyone else who needs it as an example. And thank you guys again for the help!

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>

using namespace std;

void pause()
{   
	cout << "\n Press enter to continue..."; //pause function
	char junk;
	cin.ignore();
	cin.get(junk);
}


int main()
{
	// declare constants


	// declare variables
	ifstream myInfile1;
	ifstream myInfile2;
	ofstream myOutfile;
	
	string Items, partNumber, partColor, partDescript, price;
	
	string Customer, lastName, firstName, date; 


	// get input
	
	//myInfile1.open("c:\\c++\\Customer.csv"); //openfiles  MAKE SURE FILE NAMES AND DIRECTORIES MATCH!!!
	//myInfile2.open("c:\\c++\\Items.dat");
	//myOutfile.open("c:\\c++\\Transaction.xml"); 

 
	myInfile1.open("c:\\c++\\Items.dat"); // open Items.dat which is a fixed length file
	getline(myInfile1, Items);  // take entire line in file and save it as "Items"
	cout << "\n\n" << Items; // display what is now saved for "Items"

	partNumber = Items.substr (0,8); // partNumber = first 8 characters starting at read marker 0
	cout << "\n\n" << partNumber; // display partNumber

	partColor = Items.substr (5,1);  //partColor is the 5th character in the string "Items"
	cout << "\n\n" << partColor; // display part color

	partDescript = Items.substr (8,14); // partDescript starts at read marker 8 and continues for the next 14 characters
	cout << "\n\n" << partDescript; // display partDescript

	price = Items.substr (23,8); // price starts at the 23rd read marker and continues for 8 characters
	cout << "\n\n" << price <<endl; // display price

	myInfile1.close(); //close Items.dat

	


	myInfile2.open("c:\\c++\\Customer.csv"); //open Customer.csv
	getline(myInfile2, Customer); //take entire line in file and save it as "Customer"
	cout << "\n\n" << Customer; //display what is saved for Customer


    int currentcount = Customer.find(','); // currentcount finds the first comma in the string :"Customer"
	
	lastName = Customer.substr(0,currentcount); // lastName is read marker zero to the first comma   
	  cout<< "\n\n" << lastName;// display lastName

	Customer = Customer.substr(currentcount +2,Customer.length()-currentcount+2); //Remove the first value and two following characters from "Customer" 
      cout << "\n\n" << Customer; //display new value in "Customer"
    
    firstName = Customer.substr(0,currentcount-2);  // firstName is read marker zero to the first comma   
	  cout << "\n\n" << firstName;// display firstName

	Customer = Customer.substr(currentcount,Customer.length()-currentcount); //Remove the first value and two following characters from "Customer"
	  cout << "\n\n" << Customer;//display new value in "Customer"

	partNumber = Customer.substr(0,currentcount+2)// last name is read marker zero to the first comma   
      cout << "\n\n" << partNumber;// display partNumber

	Customer = Customer.substr(currentcount+4,Customer.length()-currentcount+2); //Remove the first value and two following characters from "Customer"
	  cout << "\n\n" << Customer;//display new value in "Customer"

	date = Customer.substr(0,currentcount);// last name is read marker zero to the first comma   
	  cout << "\n\n" << date;// display date

	Customer = Customer.substr(currentcount,Customer.length()-currentcount+2);//Remove the first value and two following characters from "Customer"
	  cout << Customer; //display new value in "Customer" (which at this point should not display anything)

		myInfile2.close(); //close Customer.csv


   //output everything to xml

		//(incomplete)



	// save results to outfile





	pause();
	return 0;
}

notes:

inputfile1 is a fixed length file with the contents:

PC12K2RTUsed Car 02350.00

inputfile2 is a csv file with the contents:

Bright, Rich, PC12K2RT, 10/21/2011

the intentions of this code are to take data from both files and input them into an xml file. The xml portion is incomplete as it is independant of the programmer literally meaning it can be anything. The code is the important part. Thanks again

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.