0

I have these 2 related files:

PERSON.H::
#include<iostream>
#include<string.h>
using namespace std;
class Person
{

    private:
        string Name;
        string Surname;
        float Height;
        long long PhoneNo;
        string Nickname;
    public:
        Person(string, string, float, long long, string);

        inline string getName(){return Name;}
        inline string getSurname(){return Surname;}
        inline float getHeight(){return Height;}
        inline long long getPhoneNo(){return PhoneNo;}
        inline string getNickname(){return Nickname;}
        bool operator<(Person &);
        bool operator>(Person &);
        friend ostream& operator<<(ostream &,Person &);
};

PERSON.CPP
#include<iostream>
using namespace std;

#include "Person.h"
#include <vector>
#include <map>
#include <utility>

Person::Person(string s1,string s2,float f1,long long l1,string s3)
{
    this->Name=s1;
    Surname=s2;
    Height=f1;
    PhoneNo=l1;
    Nickname=s3;
}

ostream & operator<<(ostream &os,Person &p)
{
    os<<p.getName()<<" "<<p.getSurname()<<"||"<<p.getHeight()<<"||"<<p.getPhoneNo()<<"||"<<p.getNickname()<<endl;
    return os;
}


int main(int argc,char **args)
{
    int count=stoi(args[1]);
    cout<<count<<endl;
    vector <Person> vp;
    map <long long, Person> pm;
    
    for(int i=0;i<count;i++)
    {
        string name,surname,nickname,heightstr,phonenostr;
        float height;
        long long phoneno;
        cout<<"Input Name: ";
        cin>>name;
        cout<<endl;
        cout<<"Input Surname: ";
        cin>>surname;
        cout<<endl;
        cout<<"Input Height: ";
        cin>>height;
        cout<<endl;
        cout<<"Input Phone no. : ";
        cin>>phoneno;
        cout<<endl;
        cout<<"Input Nickname: ";
        cin>>nickname;
        cout<<endl;
        Person p(name,surname,height,phoneno,nickname);
        vp.push_back(p);
        pair<long long,Person> pr(phoneno,p);
        pm.insert(pr);
    }
    for(auto i=vp.begin();i!=vp.end();i++)
        cout<<*i<<endl;
    for(auto it=pm.begin();it!=pm.end();it++)
        cout<<it->first<<":"<<it->second<<endl;
    return 0;
}

However whenerver i try to run it on Codeblocks it simply gives me a :

terminate called after throwing an instance of 'std::logic_error'
  what():  basic_string::_M_construct null not valid

error on the output screen. I ran this on Linux during my classes and it working fine though.

I have changed the internal compiler to run on both c++11 and c++14 and yet i kept getting the same message. Only after replacing

int count=stoi(args[1]);
    cout<<count<<endl;

part with

int count;

cout<<"Enter number of input runs: ";
cin>>count;

made the code run on Codeblocks. Am i missing some part of the configuration in Codeblocks or am i using some compiler wrongly?

  • 2
    I expect you have not added a command line argument in your Code::Blocks debugging settings. With that said your code should check if argc is 2 or greater before using args[1] – drescherjm Dec 15 '22 at 16:31
  • The error message indicates that you are passing nullptr to a string constructor, which presumably means that `args[1]` is null. – john Dec 15 '22 at 16:33
  • your error suggests constructing a `std::string` from a null `char*`. The final element of `args` will be a null pointer which is likely the cause of your error – Alan Birtles Dec 15 '22 at 16:33
  • `#include` is not the correct header – drescherjm Dec 15 '22 at 16:39
  • 1
    `args` contains the command typed into the command line, not input given by the user when the program is running. Eg: `myprogram 42` entered into a terminal window's command prompt. Since you are using the Code::Blocks IDE, you are probably running the program through the IDE and must instruct the IDE to provide the correct arguments to the program when it is run. Not sure where that is in Code::Blocks, but Google seems to think it's in the main menu at **Project -> Set program's arguments**. – user4581301 Dec 15 '22 at 16:39
  • I am new to using Code::Blocks so could u guide me towards what i should pass in the debugging settings or what i should do to avoid having args be passed as null – Arkyaprabha Sengupta Dec 15 '22 at 16:42
  • 1
    Put this at the start if int main(): `if (argc < 2 ) { std::cout << "This program requires a command line argument" << std::endl; return -1; }` It will end the program properly and inform the user of their mistake in running the code. – drescherjm Dec 15 '22 at 16:46
  • I ran the above code and it game me the statement with a process returned -1. It told me absolutely nothing else :/ – Arkyaprabha Sengupta Dec 15 '22 at 16:53
  • Odd that the debug console window did not contain the error message. It could be that Code::Blocks does not keep the console window open after your program ended. However your code now has proper error checking. – drescherjm Dec 15 '22 at 16:54
  • This question should tell you how to add the command line argument to your IDE: [https://stackoverflow.com/questions/11888528/how-to-take-command-line-argument-in-codeblock-10-05](https://stackoverflow.com/questions/11888528/how-to-take-command-line-argument-in-codeblock-10-05) – drescherjm Dec 15 '22 at 16:56
  • 1
    Only one thing to improve in @drescherjm error-check above. It should use `std::cerr` and not `std::cout`. Error messages are the main purpose of `std::cerr`, after all. – Ben Voigt Dec 15 '22 at 17:01
  • @BenVoigt I did use std::cerr but it gave me the same output as above though. However thanks to @/drescherjrm i managed to work the code as i have stated below so thanks :) – Arkyaprabha Sengupta Dec 15 '22 at 17:30
  • This doesn't address the question, but member functions that are defined inside the class definition (e.g., `string getName(){return Name;}`) are implicitly inline; there's no need to mark them `inline`. – Pete Becker Dec 15 '22 at 18:07

0 Answers0