Why can't I enter both number and sentence?
Sep 9, 2017 at 10:03am UTC
Hello!
I searched for an answer for this everywhere but I couldn't find any.
so my question is why my does my program take only one string sentence but takes all of my int values?
Thank you for your time and answers!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main ()
{
string a[3];
int i,j[3];
for (i=0;i<3;i++)
{
cout<<"enter sentence \n" ;
getline(cin,a[i]);
cout<<"enter number \n" ;
cin>>j[i];
}
for (i=0;i<3;i++)
{
cout<<"\n" <<a[i]<<"\n" <<j[i];
}
}
Sep 9, 2017 at 10:20am UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
#include <iostream>
#include <string>
int main ()
{
std::string a[3];
int j[3];
for (int i=0; i<3; i++)
{
std::cout << "Enter sentence: " ;
getline(std::cin, a[i]);
std::cout << "enter number: " ;
std::cin >> j[i];
std::cin.ignore(1); // get rid of following '\n'
std::cout << '\n' ;
}
for (int i=0; i<3; i++)
{
std::cout << "a[" << i << "]: " << a[i]
<< "; j[" << i << "]: " << j[i] << '\n' ;
}
}
Sep 9, 2017 at 1:42pm UTC
I see thank you!
Topic archived. No new replies allowed.