c_str

I need some input on how I should go about writing a program to take in information, do some un-important stuff, and spit an answer back out.

In order to be able to make this program attractive to the user I sort of want to be able to take all of the information in in 1 line.

string target;
getline(cin,target);
int length = target.length();
char* value = new char[length];

where the target variable would be, lets say, area, and the value would be a value, so they would be saying 'the area of this square is value'..

Obviously this is hard (for someone at my experience) to do in one line, since it involves multiple types of variables, so I'm trying to figure out how to use the c_str function to move all of the numbers into the array and then deal with the integer value of them there, while the string target, being 'area' stays the same so I can work with all of this better.

bottomline, I can't use the c_str() function on a non constant variable, such as std::string target.

Input, thoughts, suggestions?
Last edited on
billywilliam
In order to be able to make this program attractive to the user I sort of want to be able to take all of the information in in 1 line.


If your program requires two inputs, then it needs two inputs. Unless they can be calculated or dug out of the OS then you'll need them from the user. Also in order to be usable you'll need to specify to the user what those inputs are. So, although I think your goal is possible it may end up being less intuative then you imagine.

I don't think you'll need to dynamically allocate your array. From what I can tell you should be able to get away with pointing "value" at "target" if that's really what you intend to do anyway.

I think we need more data so that we know what variables you'll need and which ones you won't before we can actually help you. What are you trying to do, try to be specific please.
Last edited on
Alright, I was trying to only give the needed information but I guess this is fairly complex.

I'm writing a program to output information about polygons such as perimeter, area,apothem, etc., but for now I'm keeping it simple and just working with squares.
I've got a 'class square' with private fields such as Asidelength, Bsidelength, Area, Perimeter.
Standard getters and setters for each, with public member functions for calculating Area and Perimeter.

the crux of the matter is getting the user to enter 'Aside 46' all in one line, and then sending a string, 'Aside', and the value '46' to a Setter. I'm finding it fairly difficult to separate the 46 and the Aside into an integer and a string respectively.

There's the core of the problem. Everything else is easy to me.
This wont be of any use when you move onto polygons but the thing about squares is that all of the sides are the same so there's no need to differentiate A-side from B-side. Or does 'class square' really mean a rectangle?
but that's not the problem. There is no confusion on how to actually structure this program or how it needs to work. I can and have done that all by myself /big boy britches. The only problem is getting the input from the user so the program will be able to calculate the area/perimeter.

I could separate it into 2 lines to make it easer.
std::string target;
int value;
cin >> target;
cin >> value;
SetInformation(target,value); //where SetInformation is a public member function of class square

that right there is a valid solution to my problem, i'm just wondering if there is a way to do those things all in one input line.
Topic archived. No new replies allowed.