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

  • Home
  • SEARCH
  • 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 8557817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:39:38+00:00 2026-06-11T15:39:38+00:00

This program is supposed to store each word given in standard input stream and

  • 0

This program is supposed to store each word given in standard input stream and count their occurences. The results are supposed to be printed afterwards in order followed by their count. As far as I can tell, the program is working otherwise, but the strings are printed as a ASCII values of the characters instead of the characters themselves. What’s wrong?

#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <cctype>
#include <algorithm>

std::string get_word();

int main()
{
    std::vector<std::string> words;
    std::string word;

    while (std::cin.good()) {
        word = get_word();

        if (word.size() > 0)
            words.push_back(word);
    }

    std::sort(words.begin(), words.end());

    unsigned n, i = 0;

    while (i < words.size()) {
        word = words[i];
        n = 1;

        while (++i < words.size() and words[i] == word)
            ++n;

        std::cout << word << ' ' << n << std::endl;
    }
}


std::string get_word()
{
    while (std::cin.good() and !std::isalpha(std::cin.peek()))
        std::cin.get();

    std::stringstream builder;

    while (std::cin.good() and std::isalpha(std::cin.peek()))
        builder << std::cin.get();

    return builder.str();
}
  • 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-06-11T15:39:39+00:00Added an answer on June 11, 2026 at 3:39 pm

    std::istream::get() doesn’t return a char but std::ios::int_type (a typedef for some integer type that can hold all values of char_type and EOF) and that is what you insert in stringstream. You should cast the result to char.

    std::basic_istream::get

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

Sidebar

Related Questions

I am supposed to modified this Sierpinski's triangle program to count the number of
This program is supposed to determine how many units are stored in the value
I wrote this short program which has a tiny GUI. Its supposed to allow
This program stores pairs in a map, counting the number of times a word
Say suppose I am running a java program through command line. And this program
This program builds a dictionary out of a list based on what two index
This program returns: package main import ( flag fmt ) func main() { num_agents
This program runs just fine, but for some reason when I ask for an
this program is for class to display the database in Listview.i want to add
This program I use has it's own variables to set when you run it,

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.