Solved

char pointer to accept string input

Posted on 2011-03-01
4
359 Views
Last Modified: 2012-05-11
Please tell me how to correct the following code:

char *s;
	scanf("%s", s);

Open in new window

0
Comment
Question by:dshrenik
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 31

Accepted Solution

by:
Zoppo earned 500 total points
ID: 35006875
Hi dshrenik,

you have to allocate a memory for the buffer before you can use it with scanf, i.e.:

char* s = new char[ 32 ];
scanf( "%s", s );
...
delete [] s;

Further you should check that the length of the buffer is used to avoid the memory buffer is to small (so the memory behind the buffer is overwritten) by adding the length - 1 to the format string, i.e.

scanf( "%31s", s );


Beside this you should even take a look at standard input stream std::cin for alternative (C++ standard) text input mechanism.

Hope that helps,

ZOPPO
0
 

Author Comment

by:dshrenik
ID: 35006891
Is there any way I can have variable that adjusts its size according to the size of the input?
Thanks!
0
 
LVL 31

Assisted Solution

by:Zoppo
Zoppo earned 500 total points
ID: 35007026
In C++ you can use std::string and std::cin.

There are two different ways for getting user input:

1. Get a simple string without whitespace (space\tab):

std::string strInput;
std::cout << "Enter some text (without spaces): ";
std::cin >> strInput;
std::cout << "Entered text: '" << strInput << "'" << std::endl;

Open in new window


2. Get a line of text which even may contain whitespaces:

std::string strInput;
std::cout << "Enter some text (with or without spaces): ";
std::getline( std::cin, strInput, '\n' );
std::cout << "Entered text: '" << strInput << "'" << std::endl;

Open in new window


Hope that helps,

ZOPPO
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 35007040
Sorry, I forgot to mention that you'll have to add these includes before you can use this:

#include <string>
#include <iostream>

Open in new window


ZOPPO
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question