Solved

char pointer to accept string input

Posted on 2011-03-01
4
355 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
  • 3
4 Comments
 
LVL 30

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 30

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 30

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now