-3

I'd like to do the following, but without including string:

#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1,s2;
char c;
cout<<"input a string: "<<endl;
cin>>s1;
cout<<"input a character: "<<endl;
cin>>c;
cout<<"input another string: "<<endl;
cin>>s2;
}

When I try this:

#include <iostream>
using namespace std;
int main()
{
char s1[128], s2[128];
char c;
cout << "input a string: " << endl;
cin.getline(s1, 128);
cout << "input a character: " << endl;
cin >> c;
cout << "input another string: " << endl;
cin.getline(s2, 128);
}

I run into issues... basically when I enter the character it also enters for the second string, and I never get a chance to enter it. please help thanks.

Rik

Rik
  • 1,870
  • 3
  • 22
  • 35
  • " but without including string" - why? –  Oct 04 '18 at 23:04
  • to learn how it works – Rik Oct 04 '18 at 23:08
  • 1
    The problem seems to be very obvious. Neither the first nor the second code chunk is valid C++, and no self-respecting C++ compiler will accept the code without errors. Neither of them comply with the requirements for a [mcve] as explained in stackoverflow.com's [help]. – Sam Varshavchik Oct 04 '18 at 23:15
  • forgot to put main in there, is that what you are talking about sam? it compiles fine – Rik Oct 04 '18 at 23:19
  • @Rik *to learn how it works* -- I see no attempt in creating a string class. – PaulMcKenzie Oct 04 '18 at 23:19
  • It is semi-obvious. It took longer to find the duplicate answer than to realize what was going wrong. – user4581301 Oct 04 '18 at 23:30

1 Answers1

-1

Use cin.get() after use "cin >> var". This method extract symbol new line from buffe

TyupkinV
  • 45
  • 5