Jump to content
  • Advertisement
Sign in to follow this  
kingpinzs

can c-string's be created dynamicaly

This topic is 3035 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

can I creat a c-string synamicaly since I dont know how much data will be entered. Is there a way to let the user enter a bunch of data and the read the size to creat a c-string?

Share this post


Link to post
Share on other sites
Advertisement
I'm guessing english isn't your first language as you're from germany.

Could you try explaining your problem/question a little more in depth? It could make perfect sense to others, but I'm a little confused on what you're actually asking for.

Share this post


Link to post
Share on other sites
instead of

const int SIZE = 100;
char input[SIZE];

cout<<"Enter a string ";
cin.getline(input, SIZE);

I want the user to be able to input a string as long as they want then I creat the char to use it.

some thing like

char *input;

cin<< as much data as they want

input = new char(x.size)

Share this post


Link to post
Share on other sites
Quote:
Original post by phresnel
In C++?


I know I can use a string but I want to use a c-string
its easyer for me to minipulate a c-string then a string class

Share this post


Link to post
Share on other sites
Quote:
Original post by kingpinzs
Quote:
Original post by phresnel
In C++?


I know I can use a string but I want to use a c-string
its easyer for me to minipulate a c-string then a string class


So easy that your code is bugfull and dangerous, open to many kinds of malicious attacks. Something that nobody should install on his machine.

Just use string and be done:

#include <iostream>
#include <string>
int main () {
std::string name;
std::getline(std::cin, name);
}



For formatting strings, use stringstream:

#include <sstream>
int main () {
std::stringstream ss;
ss << 42 << 0.5f << 0.5 << "hello world" << '\n';
std::cout << ss.str();
}

Share this post


Link to post
Share on other sites
Quote:
I know I can use a string but I want to use a c-string
its easyer for me to minipulate a c-string then a string class
std::string is most likely exactly what you are looking for.

Can you give an example of how C strings are easier to manipulate? Almost any string-related operation I can think of is simpler, safer, and takes fewer lines of code with string than with a raw character array.

(I'm not saying there's never a reason to prefer C-style strings, but 'easier to manipulate'? That seems a bit odd.)

Share this post


Link to post
Share on other sites
Quote:
Original post by phresnel
Quote:
Original post by kingpinzs
Quote:
Original post by phresnel
In C++?


I know I can use a string but I want to use a c-string
its easyer for me to minipulate a c-string then a string class


So easy that your code is bugfull and dangerous, open to many kinds of malicious attacks. Something that nobody should install on his machine.

Just use string and be done:

}


First of all this is personal use and not to be used by any one else.
Its a consule app to learn how c++ works
Also in my c++ class in collage we had to do a full chapter on c-string and I was just wondering if they could be created dynamicaly

and third


char menu();
int vowel_counter(char *);
int consonants_counter(char *);
void cls( HANDLE );

int main()
{
const int SIZE = 100;
char input[SIZE];
char menu_input;

cout<<"Enter a string ";
cin.getline(input, SIZE);


menu_input = menu();

while(toupper(menu_input) != 'E')
{
if(toupper(menu_input) == 'A')
{
cout<<"The number of vowels in your string is "<<vowel_counter(input)<<endl;
cout<<"Press a key to continue"<<endl;
cin.ignore();
cin.get();
}
else if(toupper(menu_input) == 'B')
{
cout<<"the number of consonants in your string is "<<consonants_counter(input)<<endl;
cout<<"Press a key to continue"<<endl;
cin.ignore();
cin.get();
}
else if(toupper(menu_input) == 'C')
{
cout<<"The count of both consonants and vowels is "<<vowel_counter(input) + consonants_counter(input)<<endl;
cout<<"Press a key to continue"<<endl;
cin.ignore();
cin.get();
}
else if(toupper(menu_input) == 'D')
{
cout<<"Enter a string ";
cin.ignore();
cin.getline(input, SIZE);
}

menu_input = menu();
}

cout<<"Exiting the program "<<endl;
system("PAUSE");
return 1;
}


char menu()
{
HANDLE hConsole; hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
cls(hConsole);
char input;
cout<<"\t\tCount Vowels, Consonants or Both."<<endl<<endl;
cout<<"\t A) Count the number of vowels in the string"<<endl
<<"\t B) Count the number of consonants in the string"<<endl
<<"\t C) Count both the vowels and consonants int the string"<<endl
<<"\t D) Enter another string"<<endl
<<"\t E) Exit the program"<<endl<<endl
<<"\t Enter a letter ";
cin>>input;
return input;
}

int vowel_counter(char *cString)
{

char vowles[5]={'A', 'E','I', 'O', 'U'};
int counter = 0;

while(*cString != '\0')
{
for(int i = 0; i<5; i++)
{
if(toupper(*cString) == vowles)
{
counter++;
}
}
cString++;
}

return counter;
}

int consonants_counter(char *cString)
{
char consonants[21]={'B', 'C','D', 'F', 'G', 'H' ,'J','K','L','M','N','P','Q','R','S','T','V','W','X','Y','Z'};
int counter = 0;

while(*cString != '\0')
{
for(int i = 0; i<21; i++)
{
if(toupper(*cString) == consonants)
{
counter++;
}
}

cString++;
}

return counter;
}



This is how it is easyer to use

Share this post


Link to post
Share on other sites
Quote:
Original post by kingpinzs
Its a consule app to learn how c++ works

This is how C works, to which C++ is mostly compatible.


Quote:
Also in my c++ class in collage we had to do a full chapter on c-string and I was just wondering if they could be created dynamicaly

If this is a 101 class, than they shouldn't be teaching raw string manipulation.

But if you really need it, you should at least use the "n" versions from <cstring>. And the only way for doing what you want (that is, reading in an arbitrary sized string, i.e. where a SIZE-constant is conceptually wrong) is char-wise reading, and keeping the input characters in a container, until '\n' is read.


Quote:
and third

*** Source Snippet Removed ***

This is how it is easyer to use

No, that's not easier.

Also, programs shouldn't halt once they are finished, especially not through spawning a new process.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!