Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7442703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:12:57+00:00 2026-05-29T11:12:57+00:00

I’m working on a project for a data structures class. The goal is to

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T11:12:58+00:00Added an answer on May 29, 2026 at 11:12 am

    That is because input ends when it encounters a space, that is it reads only the first number. Use cin.getline() instead.

    The exact input line would be: getline(cin, str), because you are using std::string otherwise, the cin.getline() would have done the job too.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I have a jquery bug and I've been looking for hours now, I can't
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I want to construct a data frame in an Rcpp function, but when I
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm working with an upstream system that sometimes sends me text destined for HTML/XML

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.