My code keeps crashing one line needs to be fixed

My code keeps crashing can someone fix it, it is from line 116. I put multiple asterisks where the problem is.

#include<iostream>
#include<fstream>
#include<sstream>
#include<vector>
#include<string>

using namespace std;

int count = 0;

struct Person{
string name;
int age;
};

int Menu() {
int choice;
cout << "1. Add a name" << endl;
cout << "2. Remove a name" << endl;
cout << "3. Display names " << endl;
cout << "4. Search a name" << endl;
cout << "5. Sort names" << endl;
cout << "6. Show all last names" << endl;
cout << "0. Quit\n" << endl;
cout << "Enter a option: ";
cin >> choice;

return choice;
}

void getName(struct Person *p1){

cin.ignore(1);
cout << "\nEnter Full name: ";
getline(cin,p1[count].name);
cout << "Enter age: ";
cin >> p1[count].age;
count++;
}

int findName(struct Person *p1, string list){

for (int i = 0; i < count; i++){

if (p1[i].name == list)
return i;
}
return -1;
}

int removeName(struct Person *p1, string list) {

int find = findName(p1,list);

if (find == -1)
return find;

for (int i = find; i<count; i++){
p1[i].name = p1[i+1].name;
p1[i].age = p1[i+1].age;
}

count = count - 1;
return count;
}

void displayName(struct Person *p1){

if(count == 0){
cout << "\nNo names to display\n" << endl;
return;
}

cout<<"\nName\tAge";

for(int i = 0; i<count; i++){
cout << p1[i].name <<"\t"<<p1[i].age<< endl;
}
}

void sortName(struct Person *p1){

for(int i = 0; i < count; i++){
for(int j = i + 1; j <count-1; j++){
if (p1[i].name > p1[j].name){

string temp = p1[i].name;
p1[i].name = p1[j].name;
p1[j].name = temp;
}
}
}

for (int i = 0; i < count; i++)
cout <<"\n"<< p1[i].name << endl;
}

string getLastNames(struct Person *p1){

char fileName[32];

cout<<"Enter the file name: ";
cin>>fileName;
cin.ignore(1);

ofstream fout(fileName, ios::app);
stringstream ss;

for (int i = 0; i<count;i++){

string line;
string surname;
line = p1[i].name.at(i);

int pos = line.find(', ', 0); ***********
surname = line.substr(0, pos-1);
ss << '"' << surname << '"'<<' ';
}
fout.close();
}

int main()
{
struct Person *p1;

int count = 0;
int choice = Menu();

while(choice != 0){
switch(choice){

case 1: getName(p1);
break;

case 2: {
string name;
cout << "Enter name to remove: ";
getline(cin, name);

int x = removeName(p1, name);

if (x == -1)
cout << "Name is not in the list" << endl;
else
count = x;
}
break;

case 3: displayName(p1);
break;

case 4:{
string name;

cout << "Enter name to find: ";
getline(cin, name);

int num = findName(p1, name);

if (num == -1){
cout << "Name not found in the list" << endl << endl;
}
else {
cout << "Name found at number " << num << " in the list." << endl << endl;
}
}
break;

case 5: sortName(p1);
break;

case 6: getLastNames(p1);
cout<<"Names Are Displayed in .txt file";
break;

default: cout<<"\nInvalid option";
break;
}

choice = Menu();
}
return 0;
}
Last edited on
closed account (48T7M4Gy)
First step is to edit your post and use the toolbox on the right hand side.

Then select your code and press the <> button.

That way line numbers etc will appear :)
Look at the compiler warnings / errors

At cpp.sh
115:21: warning: multi-character character constant [-Wmultichar] In function 'std::string getLastNames(Person*)':
115:28: warning: overflow in implicit constant conversion [-Woverflow] 
115:41: error: no match for 'operator*' (operand type is 'std::string {aka std::basic_string<char>}') 
120:1: warning: no return statement in function returning non-void [-Wreturn-type]


VS 2015


Error (active)	no instance of overloaded function "getline" matches the argument list
main.cpp	35	            argument types are: (std::istream, <error-type>)
Error (active)		"count" is ambiguous	main.cpp	35
Error (active)		"count" is ambiguous	main.cpp	37
Error (active)		"count" is ambiguous	main.cpp	38
Error (active)		"count" is ambiguous	main.cpp	43
Error (active)		"count" is ambiguous	main.cpp	58
Error (active)		"count" is ambiguous	main.cpp	63
Error (active)		"count" is ambiguous	main.cpp	63
Error (active)		"count" is ambiguous	main.cpp	64
Error (active)		"count" is ambiguous	main.cpp	69
Error (active)		"count" is ambiguous	main.cpp	76
Error (active)		"count" is ambiguous	main.cpp	83
Error (active)		"count" is ambiguous	main.cpp	84
Error (active)		"count" is ambiguous	main.cpp	94
Error (active)		"count" is ambiguous	main.cpp	109
Error (active)		no operator "*" matches these operands	main.cpp	115
Error	C2872	'count': ambiguous symbol	main.cpp	35
Error	C2872	'count': ambiguous symbol	main.cpp	37
Error	C2872	'count': ambiguous symbol	main.cpp	38
Error	C2872	'count': ambiguous symbol	main.cpp	43
Error	C2872	'count': ambiguous symbol	main.cpp	58
Error	C2872	'count': ambiguous symbol	main.cpp	63
Error	C2872	'count': ambiguous symbol	main.cpp	64
Error	C2872	'count': ambiguous symbol	main.cpp	69
Error	C2872	'count': ambiguous symbol	main.cpp	76
Error	C2872	'count': ambiguous symbol	main.cpp	83
Error	C2872	'count': ambiguous symbol	main.cpp	84
Error	C2872	'count': ambiguous symbol	main.cpp	94
Error	C2872	'count': ambiguous symbol	main.cpp	109
Warning	C4305	'argument': truncation from 'int' to 'char'	main.cpp	115
Warning	C4309	'argument': truncation of constant value	main.cpp	115
Error	C2100	illegal indirection	main.cpp	116
closed account (48T7M4Gy)
This is a start to how you need to rethink your data management and construction. If you use a vecor instead of an array you'll find it a little simple.

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include<iostream>
#include<string>

struct Person{
    std::string name;
    int age;
};

int Menu() {
    int choice;
    std::cout
    << "1. Add a name\n"
    << "2. Remove a name\n"
    << "3. Display names\n"
    << "4. Search a name\n"
    << "5. Sort names\n"
    << "6. Show all last names\n"
    << "0. Quit\n"
    << "Enter a option: ";
    
    std::cin >> choice;
    
    return choice;
}

void getName(Person p[], int &current_count)
{
    std::string name;
    int age;
    
    std::cin.ignore();
    std::cin.clear();
    
    std::cout << "Enter Full name: ";
    getline(std::cin, name);
    std::cout << "Enter age: ";
    std::cin >> age;
    
    
    current_count++;
    p[current_count].name = name;
    p[current_count].age = age;
}


void displayNames( Person p[], int count)
{
    if(count == 0)
    {
        std::cout << "No names to display\n";
        return;
    }
    else
    {
        std::cout<<"Name\tAge";
        for(int i = 0; i <= count; i++)
        {
            std::cout << p[i].name << '\t' <<p[i].age<< '\n';
        }
    }
}

int main()
{
    const int no_people = 10;
    Person data[no_people]; // This could be a vector
    
    int current_no_people = 0;
    int choice = Menu();
    
    while(choice != 0){
        switch(choice){
                
            case 1: getName(data, current_no_people );
                break;
            case 3: displayNames(data, current_no_people);
                break;
                
            default:
                std::cout<<"Invalid option\n";
                break;
        }
        choice = Menu();
    }
    return 0;
}
Hello jdeep,

As Thomas1965 has shown you there are many errors in your program. When I loaded your program in my VS 2015 the first compile gave 57 errors.

I found the "count" being ambiguous error came from the line "using namespace std;". Along the way one of the header files includes the "utility" header file which has a "count" function in the "std" namespace,so the compiler does which version of "count" to use. After I removed the line "using namespace std;" and qualified things like "cout", "cin", "endl" and some others in the "std" namespace with "std::" snd most of the errors disappeared.

In main you have the line struct Person *p1;, but you never give p1 a value i.e., an address to use. If you want to use a pointer, I believe what you will need is:
1
2
struct Person p;
struct Person *p1 = &p;

or you could just use the "p" variable instead of the pointer.

I still want to do some work to see what I can come up with. For now this is what I have found.

Hope that helps,

Andy
Hello jdeep,

In all your functions except "getName" you are using subscripts on p1, but p1 is not an array. You have not defined any kind of array in your program. I would suggest using a vector to store each "p1" struct.

The easiest parts to work on would be case 1 and case 3. Once you get these parts working the rest will be easier to fix.

For what it is worth I am having trouble with "include <cmath>" not being found, but this comes from other "include"s that are not part of what is in the main file, but are included because of what is in main.

Hope that helps,

Andy
Topic archived. No new replies allowed.