I'm working on a project for a data structures class. The goal is to use a tree to figure out if metaphorical dwarfs can pay their metaphorical taxes with metaphorical diamonds. Input is given as a string in the format "3 1 2 3 3 1 2 3" which implies 3 diamonds of value 1 2 3 and 3 taxes cost of 1 2 3.
The issue I am having does not descend from the implementation of the tree but dealing with parsing the input into a way that I can insert it into the tree and such, specifically: When I input values in the code directly when testing it gives the correct output, but when using cin the output run into a few problems.
With the following code:
string str;
str = "5 1 2 3 4 5 5 1 2 3 4 5";
str.erase(remove_if(str.begin(), str.end(), ::isspace), str.end());
char ch[str.size()];
strcpy(ch, str.c_str());
int numDiamonds = ch[0] - '0';
cout<<numDiamonds<<" diamonds"<<endl;
int counter = 1;
for(int i = 1; i < numDiamonds+1; ++i){
int out = ch[i] - '0';
cout<<out;
cout<<" ";
++counter;
}
cout<<endl;
int numTaxes = ch[numDiamonds+1] - '0';
cout<<numTaxes<<" taxes"<<endl;
for(int i = counter+1; i < str.size(); ++i){
int out = ch[i] - '0';
cout<<out;
cout<<" ";
}
cout<<endl;
}
my output appear correct, and as follows:
5 diamonds
1 2 3 4 5
5 taxes
1 2 3 4 5
but when I change "str = 5 1..." to "cin >> str" my output appears jumbled and is the following.
5 diamonds
-48 -48 -48 -48 -48
-20 taxes
1 diamonds
-48
-48 taxes
2 diamonds
-48 -48
-48 taxes
3 diamonds
-48 -48 -48
-48 taxes
4 diamonds
-48 -48 -48 -48
-48 taxes
5 diamonds
-48 -48 -48 -48 -48
-20 taxes
5 diamonds
-48 -48 -48 -48 -48
-20 taxes
1 diamonds
-48
-48 taxes
2 diamonds
-48 -48
-48 taxes
3 diamonds
-48 -48 -48
-48 taxes
4 diamonds
-48 -48 -48 -48
-48 taxes
5 diamonds
-48 -48 -48 -48 -48
-20 taxes
No amount of googling has helped solve my problem, so I turn you guys. Is there an explanation as to why cin into a string has different behavior than defining the string in the code?
Thanks!