Solved

char pointer to accept string input

Posted on 2011-03-01
4
357 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 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 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.

776 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