?
Solved

char pointer to accept string input

Posted on 2011-03-01
4
Medium Priority
?
360 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 2000 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 2000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.
Suggested Courses
Course of the Month15 days, 14 hours left to enroll

741 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