#include <stdio.h>

#include <iostream>
#include <string.h>

using namespace std;
int main()
{
     string str1;
    char* str;
    cout<<" enter your command "<<endl;
    cin>>str1;
    str=(char*)str1.c_str();
    char* str2="exit";
    int status;
    while(strcmp(str,str2)!=0){
      // createChildProcess(str);
      cout<<str<<endl;
      cout<<str1<<endl;

      cout<<strlen(str)<<endl;


       cout<<" enter your command "<<endl;
       cin>>str;
    }

  return 0;
}

try to run this program and tell me why the output is it?

as i tried to enter

good day

and the output becomes

good
good
4
enter your command
1
1od
1

but i supposed the output will
good 1
good 1
6
enter your command

i just want to know how the program run.

Recommended Answers

All 5 Replies

line 12: you can't use cin >> operator for strings that contain white space (space or tabs). To do that you have to call getline(), e.g. getline(cin,str1);

Member Avatar for Ryanzp

line12 binary '>>' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or there is no acceptable conversion
)
the same line19

Ryanzp is correct. you need to include <string> to get std::string declared.

and what's the difference between
include <string.h> and <string> as i was already included string.h .

<string.h> is a C-compliant headers that provide functions for comparing, copying, and doing other things to arrays of characters (For example, "char* foo" or "char buf[128]"). strcmp(), strcpy(), and whatnot are included from there. They actually don't do anything with the "string" class.

<string> is for C++, and that is what defines the actual "string" class, which is much more advanced. If you're using both, just include both.

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.