Hi, I'm facing a problem.
I need the user to input a string, and then I have to add the string character by character to a queue so I can perform some parsing on it, I'm doing something wrong that I don't know.

I've used sizeof(), length() and strlen() built in functions for the for loop to enter the string character by character, but it keeps giving me debugging errors. I'm new to this. Hope someone can point out my coding mistakes

char* sntnc;
cout<<"Enter sentence: ";
cin>>sntnc;

int f = strlen(sntnc);

for(int g=0; g<f; g++)
{
	q.add_q(sntnc[g]);
}

Recommended Answers

All 3 Replies

>>char* sntnc;
That is just an unallocated pointer. If you want to use pointers then you have to allocate memory for it. char* sntnc = new char[255]; For the purpose of your program a better way is not to use a pointer char sntnc[255]; Or even better yet, use std::string std::string sntnc; >>cin>>sntnc;
The >> operator will not work if you want the user to enter some text that includes spaces. Use getline() instead. There are two versions of getline() -- one for std::string and the other for char*.

Thank you. I've tried the following and it works fine I guess:

char sntnc[10];
cout<<"Enter sentence: ";
cin.get(sntnc, '\n');
int f = strlen(sntnc);
...

is it fine to do this?

>>char* sntnc;
That is just an unallocated pointer. If you want to use pointers then you have to allocate memory for it. char* sntnc = new char[255]; For the purpose of your program a better way is not to use a pointer char sntnc[255]; Or even better yet, use std::string std::string sntnc; >>cin>>sntnc;
The >> operator will not work if you want the user to enter some text that includes spaces. Use getline() instead. There are two versions of getline() -- one for std::string and the other for char*.

and I have another question please.

while(!q.is_empty())
	{	
		tbl(st.top(x),q.front_el(y));
	}

I have a stack that contains number, and a queue with the characters entered.
the function tbl is a table, the intersection of some rows and cloumns decide whether to perform r() function or s(). sometimes I push a number to the stack, sometimes pop it, and I keep deleting the first element in the queue. anyway. I don't know if you undcerstand. but is the above code right for doing so???

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.