0
#include<iostream>
#include<string>
#include<stdio.h>
#include<stdlib.h>
//#include<set>
#include<list>
#include<cmath>
//#include<algorithm>
#include<fstream>
#include<vector>

#define max 10000.0
using namespace std;

int main()
{
    int n;//number of rooms
    cin>>n;
    vector<string> room_name(n,"");

    for(int i=0;i<n;i++)
        {
            string tmp;
            cin >> tmp;
            room_name.push_back(tmp);
        }
}

The error Segmentation fault: 11 still occurs.

Unable to find the point of error. Any idea why this might be happening?

4
  • 4
    thats not how you add things into a vector... Commented Jun 1, 2015 at 14:36
  • possible duplicate of Segmentation fault in C++ using vectors Commented Jun 1, 2015 at 14:44
  • Edited the above question with answers obtained. Still getting an error after applying those changes in a parent function. Commented Jun 1, 2015 at 15:20
  • 2
    @Archit Editing a question after obtaining an answer is not a good way obtaining help on Stack Overflow, because such editing invalidates the answers you've got. If solving an original problem brings you to solving a different problem, try solving that problem independently, or post another question. Commented Jun 1, 2015 at 15:23

3 Answers 3

2

It is not enough to declare a vector: unlike an array, it could grow, so by default it starts with a fixed size of zero.

If you know the size of the vector upfront, you can request a vector of a specific size (you can grow it after that, too)

vector<string> room_name(n, ""); // You can supply a different default, or no default here

This makes a vector of 100 empty strings, letting you access elements 0..n-1 in the subsequent loop.

Alternatively, you could make your vector grow one element at a time by using push_back, like this:

for(int i=0;i<n;i++)
    {
    string tmp;
    cin >> tmp;
    room_name.push_back(tmp);
    }
Sign up to request clarification or add additional context in comments.

6 Comments

thanks, the string input problem did get solved. However, using it in the parent function still gives errors. Any clues to why that might be happening. Have edited the question above.
Where is the "parent function"? You've got only main() there.
There are some statements above the function that I have added into the main function itself. Running this gives the same segmentation fault error
float xs[4],ys[4]; for(int i=0;i<4;i++) cin>>xs[i]>>ys[i]; int n; cin>>n; Added lines of code.
i still get the same error. Can't seem to understand how two more cin statements would bring in the same error :/
|
1

Unless the vector is already sized, you need to use push_back to append to the end of the vector.

std::vector<int> foo;
for (int i = 0; i < 10; ++i)
{
    foo.push_back(i);
}

Otherwise you need to ensure that the vector is already sized, either

std::vector<int> foo(10);

Or

std::vector<int> foo;
foo.resize(10);

Then you could use indexing as you did

for (int i = 0; i < 10; ++i)
{
    foo[i] = i;
}

If you try to write to each index before you sized your array, you are writing to memory out of bounds.

Comments

0

Are you going indeed to enter 1000 strings manually in the vector?:)

Also it is a good idea to define canstant names using letters of upper case. So instead of

#define max 10000.0

it would be better to write

#define MAX 10000.0

And it is not clear why max is defined as a float number instead of an integer.

Take into account that variable n was not declared in your program. So the program does not compile. Maybe you mean max instead of n.

As the vector is defined as an empty vector

vector<string> room_name;

that is it does not have elements then you may not use the subscript operator to enter values in the vector.

You could use method push_back instead the following way

vector<string> room_name;
room_name.reserve( n );

for ( int i = 0; i < n; i++ )
{
    string s;

    cin >> s;

    room_name.push_back( s );
}

And instead of the loop you could use standard algorithm std::copy_n declared in header <algorithm>

#include <iostream>
#include <string>
#include <iterator>
#include <algorithm>

#define N   2

int main() 
{
    std::vector<std::string>room_name;
    room_name.reserve( N );

    std::copy_n( std::istream_iterator<std::string>( std::cin ), N,
                 std::back_inserter( room_name ) );

    for ( const std::string &s : room_name ) std::cout << s << std::endl;                

    return 0;
}

If to enter strings

my_room
your_room

then the program will output them from the vector

my_room
your_room

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.