0

this is a basic question but I'm new to C++ so apologies in advance :)

I couldn't seem to print out the strings I stored in a vector. I have used std:: cout as well as printf but printf seems to give the error "The program has stopped working". Where am I going wrong?

Here's the code with std::cout :-

   #include <iostream> 
   #include <cstdio>         
   #include <vector> 
   #include <fstream> 
   using namespace std;

    int main(){ 
     int np; 
     string temp;  

     scanf("%d", &np); 
     vector <int> money;
     vector <string> names;  

        for(int i = 0; i< np; i++){
          scanf("%s", &temp); 
          names.push_back(temp); 
          cout << names[i] << endl; 
       } 

   return 0;
   }

This didn't return any string at all.

The other program I tried with printf is exactly the same, except the cout line is replaced with:

printf("%s", &names[i]); 
Aashir shukla
  • 93
  • 1
  • 9
  • Use cin << to read from a string and optionally the int. scanf should not work. Its expects an array of char with %s. – Robert Jacobs Apr 04 '16 at 14:22
  • 3
    I think you need a good beginners book, [here's a list of a few](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Some programmer dude Apr 04 '16 at 14:25
  • 1
    if `scanf("%s", &temp);` *did **not*** throw a compiler warning in your face, you need to turn up your warning levels to something more pedantic. – WhozCraig Apr 04 '16 at 14:38

3 Answers3

1

You should not use scanf for reading a std::string, because %s modified accepts a char*. You also shouldn't use printf("%s", &names[i]); for printing a std::string object.

scanf and printf are C functions. There is no std::string type in the C language, so, they are operating with plain char arrays.

Instead of scanf and printf you should use std::cin and std::cout:

std::string str;
std::cin >> str; // input str
std::cout << str; // output str
awesoon
  • 32,469
  • 11
  • 74
  • 99
1

You can't use scanf() to read integers right away.

This should work:

int np;
std::string temp;

std::cout << "Enter the size: ";
std::cin >> np;
//vector <int> money;
std::vector<std::string> names;

for (int i = 0; i< np; i++) {
    std::cin >> temp;
    names.push_back(temp);
    std::cout << names[i] << endl;
}
Khalil Khalaf
  • 9,259
  • 11
  • 62
  • 104
  • Well, there is nothing wrong with reading an integer with `scanf` (although, this is not C++ way). – awesoon Apr 04 '16 at 14:25
0

There are two things you need to change about your code. First, of all, scanf() doesn't support any c++ classes. You can read more about it on this link. Second, to replace scanf(), you can use getline(cin, temp). In order to use it, you should add a line cin.ignore(); before a getline call is made because you you enter a number and press enter a '\n' character gets inserted in the cin buffer that will be use the next time you call getline.

   #include <iostream> 
   #include <cstdio>         
   #include <vector> 
   #include <fstream> 
   using namespace std;

    int main(){ 
     int np; 
     string temp;  

     scanf("%d", &np); 
     vector <int> money;
     vector <string> names;  
     cin.ignore();
        for(int i = 0; i< np; i++){
          getline(cin, temp);
          names.push_back(temp); 
          cout << names[i] << endl; 
       } 

   return 0;
   }

Look at the working demo of the code here.

I hope I was able to explain it properly.

Community
  • 1
  • 1
Shubham Khatri
  • 270,417
  • 55
  • 406
  • 400
  • Thanks a lot. :) do you think getline is more efficient than just using std::cin ? – Aashir shukla Apr 06 '16 at 14:47
  • Well you can use either of them depending upon your need. **cin**, ignores space, tabs, and newlines. In most cases, you want the invisible characters the user enters to be stored in a string as well. In that case you use getline. Refer to this link for more details: http://www.programmingincpp.com/standard-input-function.html – Shubham Khatri Apr 06 '16 at 17:19