Click here to Skip to main content
15,067,630 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm a 13 year old who's just tring to experiment with things in C++. I declared a vector for storing the contents of a file and a pointer with dynamically allocated memmory. With this, I could perhaps make some sort of an local account database. Although when comparing the two, I get thrown an exeption (Segmentation fault) did some research and I think it means I'm writing or reading from a memmory illegally?



C++
          std::string* user_details_confirmation = new std::string[3];
    
                  
                    for (int i = 0; i < 6; i++){ std::cout << " "; }
                    std::cout << "First Name : ";

                    std::cin >> user_details_confirmation[0];
                    if(user_details_confirmation[0] == setup_file_contents_vec[0]) {};
                    system("cls");
<pre lang="C++">


What I have tried:

I changed both of the variables to vectors.
Code was originally in a loop (Got rid of it).
Posted
Updated 13-Oct-21 7:00am
v3

Given that you've defined setup_file_contents_vec, but not initialized it, then setup_file_contents_vec[0] does not exist. So trying to assign a value to it results in a run-time error. You probably want to be using the std::vector push_back() method to add new items to the vector.
   
Comments
apetrai 13-Oct-21 12:55pm
   
Initializing the vector lead to my console crashing instantly.
Sorry, that wasn't meant to be a solution; just informative. Personally, I'd probably create a temp variable for the input, then use the push_back method, like k5054 suggested.

Something like this, perhaps:

#include <iostream>
#include <iomanip>
#include <vector>

using namespace std;

int main()
{
    std::vector<std::string> user_details_confirmation{};
    std::vector<std::string>setup_file_contents_vec{ "timmy", "jimmy" };
    
    for(auto& iter : setup_file_contents_vec)
    {
        cout << "   setup_file_contents_vec: " << iter << std::endl;
    }
    
    std::string input_value;
    std::cout << "First Name : ";
    std::cin >> input_value;
    
    user_details_confirmation.push_back(input_value);

    std::cout << "user_details_confirmation: " << user_details_confirmation[0] << std::endl;
    std::cout << "setup_file_contents_vec: " << setup_file_contents_vec[0] << std::endl;
    
    
    if(user_details_confirmation[0] == setup_file_contents_vec[0]) 
    {
        std::cout << "EQUAL";
    }
    
    
    return 0;
}
   
Comments
apetrai 13-Oct-21 13:04pm
   
Oh it's alright, yes, I did try this method, still got thrown an excpetion.
apetrai 13-Oct-21 13:06pm
   
If this is relevant, this is inside a class member. And the setup_file_contents_vec is inside the class. Because I use it before to define it with getting the contents of the file inside in another member.
apetrai 13-Oct-21 13:09pm
   
I have a github repo with this code. If you need additional info you can check it https://github.com/Gamerguy123lo/EXPONOS_Console
apetrai 13-Oct-21 13:49pm
   
@BrianPendleton
Where is the definition of "setup_file_contents_vec"?
Is it initialized?
   
Comments
apetrai 13-Oct-21 12:40pm
   
@BrianPendleton It's not, std::vector<std::string>setup_file_contents_vec{};
In that case, that's most likely your problem. The vector "setup_file_contents_vec" has zero elements with your definition.
   
Comments
apetrai 13-Oct-21 12:48pm
   
Although it is later defined:

std::string temp_var_to_vec;

while(setup_file_required_scan >> temp_var_to_vec) { setup_file_contents_vec.push_back(temp_var_to_vec); }
Also, I am not certain that you can read directly into a vector like that without creating that element first. This worked for me, tested here[^] :

#include <iostream>
#include <iomanip>
#include <vector>

using namespace std;

int main()
{
    std::vector<std::string> user_details_confirmation{""};
    std::vector<std::string>setup_file_contents_vec{ "timmy", "jimmy" };
    
    for(auto& iter : setup_file_contents_vec)
    {
        cout << "   setup_file_contents_vec: " << iter << std::endl;
    }
    
    std::cout << "First Name : ";
    std::cin >> user_details_confirmation[0];

    std::cout << "user_details_confirmation: " << user_details_confirmation[0] << std::endl;
    std::cout << "setup_file_contents_vec: " << setup_file_contents_vec[0] << std::endl;
    
    
    if(user_details_confirmation[0] == setup_file_contents_vec[0]) 
    {
        std::cout << "EQUAL";
    }
    
    
    return 0;
}
   
Comments
apetrai 13-Oct-21 12:56pm
   
Initializing the vector lead to my console crashing immediately.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900