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 623467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:05:45+00:00 2026-05-13T19:05:45+00:00

The program is supposed to receive an input through cin, tokenize it, and then

  • 0

The program is supposed to receive an input through cin, tokenize it, and then output each one to show me that it worked properly. It did not.

The program compiles with no errors, and takes an input, but fails to output anything.

What am I doing wrong?

int main(int argc, char* argv[])
{
  string input_line;

  while(std::cin >> input_line){
    char* pch = (char*)malloc( sizeof( char ) *(input_line.length() +1) );

    char *p = strtok(pch, " ");
    while (p != NULL) {
      printf ("Token: %s\n", p);
      p = strtok(NULL, " ");
    }
  }
  return 0;
}

I followed the code example here: http://www.cplusplus.com/reference/clibrary/cstring/strtok/

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-13T19:05:45+00:00Added an answer on May 13, 2026 at 7:05 pm

    Looks like you forget to copy the contents of input_line to pch:

    strcpy(pch, input_line.c_str());
    

    But I’m not sure why you’re doing string tokenization anyway. Doing cin >> input_line will not read a line, but a token.. so you get tokens anyway?

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

Sidebar

Related Questions

Ok, so I have a simple c++ program that's supposed to run a couple
I built a program that parses the header and I would like to read
In a Qt program, I have a QWidget class that is the superclass of
I am supposed to write a simple Cinema Booking System, which allows a customer
I wrote a simple program solving the Readers-Writers problem using semaphores. It runs perfectly
I have a two-part program where the main core needs to have an adapter
Woot, first Stack Overflow post! I've been asked to work on a desktop application
I'm a newbie to nHibernate, and Fluent nHibernate, and I'm having a great deal
I have a Visual Studio solution which consists of a DLL and an EXE.
I was recently asked to write 3 test programs for a job. They would

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.