string and array

i am trying to loop this three times and get a long string with a space in between. however the program stops whenever i try to key in the third info....why???




#include <iostream>
using namespace std;
int main ()
{

string str[3];

for (int i=1; i<=3; i++)
{
cout<<"please enter your name : ";
getline (cin, str[i]);
}

for (int i=1; i<=3; i++)
{
cout<<str[i].substr(0,7)<<endl;
}
system ("pause");
return 0;
}

@LEE PEI TING

The reason, is because arrays start at 0, not 1. Change your for loop to for (int i=0;i<3;i++) and things should work out fine.
oh...ok... thanks for that =)))


i was actually trying to use the getline
i apply on the code below but i did not get any output for it (name)... why???


#include <iostream>
#include <string>
using namespace std;

int main ()
{

int id;
int quantity;
double price;
string name;


cout << "ID : ";
cin >> id;

cout << "ITEM : ";
getline (cin, name);
cin.ignore(numeric_limits<streamsize>::max(), '\n');


cout << "PRICE : ";
cin >> price;

cout << "QUANTITY : ";
cin >> quantity;


cout <<id<<'\t'<<name<<'\t'<<price<<'\t'<<quantity<< endl<<endl;

system ("pause");
return 0;
}
The proper way to use getline is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/ istream getline
#include <iostream>
using namespace std;

int main ()
{
  char name[256], title[256];

  cout << "Enter your name: ";
  cin.getline (name,256);

  cout << "Enter your favorite movie: ";
  cin.getline (title,256);

  cout << name << "'s favorite movie is " << title;

  return 0;
}
what if i wanted to use string and not char?
1
2
 string str;
 getline (cin,str);
yea.. that is what i'm doing there but i couldn't get the output =(
This code works.

1
2
3
4
5
6
7
8
9
10
11
#include <string>
#include <iostream>

int main()
{
  std::string str;
  getline (std::cin,str);

  std::cout << "Input was: " << str;
  return 0;
}

what does ' std:: ' means?
It means that the type is part of the std namespace.

http://www.cplusplus.com/doc/tutorial/namespaces/

Let me guess; you've been sticking using namespace std; at the start of all your code with no idea what it means.
Well, actually, a string is just a group of chars.
@moschops
haha... its true... i have no idea what it means. Now i understand it =) Thanks!

@whitenite1
yes. i know this... =)



However... i can't get the output yet

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
27
28
29
30
31
32
33
34
#include <iostream>
#include <string>
using namespace std;

int main ()
{

    int id; 
    int quantity;
    double price;
    std::string name;


             cout << "ID               : "; 
             cin  >> id;
             
             cout << "ITEM             : "; 
             getline (cin, name);
             cin.ignore(numeric_limits<streamsize>::max(), '\n');

             
             cout << "PRICE            : "; 
             cin  >> price;
             
             cout << "QUANTITY         : "; 
             cin  >> quantity;
             
                         
             cout  <<id;
             cout<<'\t'<<name<<'\t'<<price<<'\t'<<quantity<< endl<<endl;
             
             system ("pause");
             return 0;
             }


this is part of my project... T.T
OK... i've got the solution =)))

should place cin.ignore() before getline(cin,name)

@whitenite1 AND @Moschops,
thanks a lot for answering my silly question.

Merry Christmas in advance! =)
Topic archived. No new replies allowed.