• Advertisement
Sign in to follow this  

Length of input array

This topic is 3625 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

Creating a simple program that reads a line of input from the console, classifies each character as an operator or operand, and performs the proper math equations. I know I can get the line of input up to the point where a user pushes the enter key into an array of chars, however I have confusion about two aspects of this. 1.) How would I determine the length of the input as I would like to loop through the character array (classify each character) 2.) How do I limit the input amount of the user so data can not be written past the end of my character array. I'm sure these are simple questions, but I do appreciate any help I can get :)

Share this post


Link to post
Share on other sites
Advertisement
Not sure if you've already deliberated over this but, rather than a character array, perhaps you should use std::string.

If you are using std::cin to read input you can read straight into a string object and then use the length() function if you want to know how big it is;

std::string temp;

std::cin >> temp;

if( temp.length() < 10 )
{
//Do somthing.
}



Becuase the size of the string is not fixed you wouldn't have to limit put, however if you did want to limit the size you could test the lenght and the user to re-enter if its above a certain size.

Not sure its what you need but hope that helps.

Share this post


Link to post
Share on other sites
(Assuming you're using C++)
Why use a character array? Why not a string? Using a string will allow you to grab an entire line from the console, and strings can be indexed like arrays. You would be able to find the length of the string easily, and if you wanted an easy way to limit the input, you could only parse the string up to a certain position (this doesn't actually limit the amount of input, but it limits how much of the input will be read/considered/parsed).

Share this post


Link to post
Share on other sites
Quote:
Original post by Shakedown
(Assuming you're using C++)


Yeah if thats not the case then please ignore me. :D

Share this post


Link to post
Share on other sites
Yes I am using c++, so strings do seem to be an easy solution. Based on your advice, should I conclude that using strings is superior to character arrays when using c++ (It seems that way just from their "built in" functions).

Also you say I can index a string just as a character array, what does the operator return when I do this.


myString; //<--does this return a string or a char?



Share this post


Link to post
Share on other sites
According to this i believe the [] operator returns a char. Well a reference to a char. But note that if you wanted to use that character, i believe that its ok to assign that character to another string object. Could someone please confirm the following;

std::string myString("Hello");

std::string myCharacter;

myCharacter = myString[0]; //myCharacter should contain "H"



The string class is part of the STL and has been created to provide a more usefull solution to charater arrays and to address some issues like you have been having. The STL is very useful and i would recommend spending some time learning about strings and perhaps other parts such as containers. Having a good book on this subject may also be beneficial.

Share this post


Link to post
Share on other sites
Sounds great, got my program up and running like a charm. The book I'm going through right now covers portions of STL, but I'll be sure to pick up a book dedicated to the matter.

Thanks very much for your help! :)

Share this post


Link to post
Share on other sites
Quote:
Original post by gsGomer
but I'll be sure to pick up a book dedicated to the matter.


I must confess that i don't actually own my own book, but i have been intending to buy this one as i have been told its a good one.

Glad to help.

Share this post


Link to post
Share on other sites
Quote:
Original post by JimmyDeemo
Could someone please confirm the following;

std::string myString("Hello");

std::string myCharacter;

myCharacter = myString[0]; //myCharacter should contain "H"



Yes, that works. Strings, while only holding a single char (in this case, myCharacter = "H") are still strings.

Quote:
Original post by gsGomer
Based on your advice, should I conclude that using strings is superior to character arrays when using c++ (It seems that way just from their "built in" functions).


Yes. For now, use std::strings whenever you're dealing with anything that might be more than a single alphabet character and/or integer. Compared to arrays, strings work the same way. The first element in the string has index of 0, and the last element of the string has index of string.length() - 1.

Share this post


Link to post
Share on other sites
Quote:
Original post by JimmyDeemo
Could someone please confirm the following;

*** Source Snippet Removed ***


Yep. You can assign a character to a string just fine.

Confusingly enough, though, this would fail to compile:


std::string myString("Hello");
std::string myCharacter = myString[0];


because in the second line, we are initializing the variable, not actually assigning to it, and that means the compiler will look for a constructor. Oddly, std::string doesn't offer a (char) constructor, although it does offer a (int, char) [okay, technically a (size_t, char) one], which initializes with however many copies of the character. So you could write


std::string myString("Hello");
std::string myCharacter(1, myString[0]);

Share this post


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

  • Advertisement