Below is a code that i have created to seperate a string statement. There are no errors but the code will not display the micron or the idustry statement. what do i need to do so that everything will show

#include <iostream>
#include <string>
#include <fstream>
#include <cstdio>
#include <cctype>
#include <iomanip>
#include <cstdlib>


using namespace std;

int main()
{
	string productNumber;

	cout << endl << setw(40) << "ENTER IN PRODUCT NUMBER: ";
	cin >> productNumber;

	string type = productNumber.substr(0,1);
	string buildCode = productNumber.substr(2,3);
	string micron = productNumber.substr(4,5);
	string lenght = productNumber.substr(6,6);
	string endone = productNumber.substr(7,7);
	string endtwo = productNumber.substr(8,8);
	string oring = productNumber.substr(9,9);
	string industry = productNumber.substr(10,10);

	cout << "\n\n\t" << type << micron << industry << endl;

	return 0;
}

thanks! another question: after i fixed the problem, only one the type was outputed and not the micron or industry string

NEW CODE:

#include <iostream>
#include <string>
#include <fstream>
#include <cstdio>
#include <cctype>
#include <iomanip>
#include <cstdlib>  

using namespace std; 

int main()
{	
string productNumber; 	

cout << endl << setw(40) << "ENTER IN PRODUCT NUMBER: ";
cin >> productNumber;

string first = productNumber.substr(0,1);
string buildCode = productNumber.substr(2,2);
string micron = productNumber.substr(4,2);
string lenght = productNumber.substr(6,1);
string endone = productNumber.substr(7,1);
string endtwo = productNumber.substr(8,1);
string oring = productNumber.substr(9,1);
string industry = productNumber.substr(10,1);

cout << "\n\n\t" << type << micron << industry << endl;

return 0;
}

A sample product number is FAP831EHSB

And the output i get is 'F'

This shouldn't even compile because you didn't declare "type". You probably meant to type "first" or "buildCode"

Second problem you have is here: string industry = productNumber.substr(10,1); You're sample-input-string only has 10 characters which means position 0-9. 10 is out of bounds.
You probably made a logic-error in this piece of code:

string first = productNumber.substr(0,1); // <-- gets only the first character
string buildCode = productNumber.substr(2,2); // <-- gets the third and fourth.

as you can see you never do anything with the second character.

Edited 6 Years Ago by Nick Evan: n/a

I fixed the problem but i still cant get anything but the "first" to show up...

string productNumber;

	cout << endl << setw(40) << "ENTER IN PRODUCT NUMBER: ";
	cin >> productNumber;

	string first = productNumber.substr(0,1);
	string buildCode = productNumber.substr(1,2); 
	string micron = productNumber.substr(3,2);
	string lenght = productNumber.substr(5,1);
	string endone = productNumber.substr(6,1);
	string endtwo = productNumber.substr(7,1);
	string oring = productNumber.substr(8,1);
	string industry = productNumber.substr(9,1);

	cout << first << " " << buildCode << " " << micron << " " << length << endl;

	return 0;
}

"length " is not declared.... The rest of the code looks fine.
Are you sure that this is the exact code you're using?

[edit]
That's about the n-th time I crossposted with iamthwee today.

Edited 6 Years Ago by Nick Evan: n/a

This article has been dead for over six months. Start a new discussion instead.