Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

char pointer to accept string input

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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…
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 goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
Suggested Courses

610 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