my problem is that how can we intialize a string using cin in output console like this.
eg.
My class is very good through cin...as the spaces put the null character so only first alphabet is initialized....kindly someone give me solution..

Recommended Answers

All 9 Replies

I'm not sure what you want. Something like this???

int main(int arc, char* argv[])
{
   std::string input;
   if( argc > 1)
      input = argv[1]; // get string from command-line argument
   else
       cin >> input; // get string from keyboard or from stdin
}

cin does not allow space in the string, so if you need the spaces you can not use cin with >>, but call getline() instead of cin

assiming input is of type std::string
getline(cin,input);

when input is char array and not a pointer

char input[80];
cin.getline(input, sizeof(input));

kindly tell
std::string input is in which library?
what is argc in the above code ,please elaborate.

std::string input; ??
if( argc > 1) ??
input = argv[1]; // get string from command-line argument

D:\Program Files\Microsoft Visual Studio\MyProjects\cin character string\b.cpp(11) : error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::alloc
ator<char> >' (or there is no acceptable conversion)
Error executing cl.exe.

the above code which u have put is not executing with the above mentioned error

Member Avatar for ArashVenus

@Builder , little bit of google search would do It
std:string is a part of string library (#include <string>)

Here is a more complete and compilable program snippet

#include <string>
#include <iostream>

int main(int arc, char* argv[])
{
   std::string input;
   if( argc > 1)
      input = argv[1]; // get string from command-line argument
   else
       std::cin >> input; // get string from keyboard or from stdin
}

argc is the number of command-line arguments plus the name of the program. On most platforms the value of argc is at least 1. argv is an array of strings that represent each of the command line arguments. argv[0] is the name of the program.

So, if the name of the program is myprog.exe and you execute it like this on the command line
myprog one two three

The value of argc will be 4.
argv[0] == "myprog.exe" (sometimes it includes the full path)
argv[1] == "one"
argv[2] == "two"
argv[3] == "three"

VB Users, always include "stdafx.h"

VB Users, always include "stdafx.h"

Why?? It's only necessary if you use Visual C++ and then only if you use precompiled headers option. Otherwise you can delete it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.