Need help with small project.

I`m going to post the essential code that I think will help fix these problems. I do not have any errors in my code. All of my choices on my menu should be working but they`re not.

Problem 1
So this is what it my list is supposed to look like when i choose choice 2.

Billy Gibbons
12/16/1949
Jimmy Page
1/9/1944
Jeff Beck
6/24/1944
George Harrison
2/25/1943
Eric Clapton
3/30/1945
Lindsey Buckingham
10/3/1949

what it ends up looking like when I choose choice 2:
illy Gibbons 16/2/1949 immy Page 1944/9/1949 eff Beck 1944/24/1949 eorge Harrison 1943/25/1949 ric Clapton 1945/30/1949 indsey Buckingham 3/0/1949

-------------------------------------------------------------------------------
Problem 2

In choice 5 the 'cout' pops up "enter the name of your friend" when i enter a name it returns:

terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
aborted (core dumped)
-------------------------------------------------------------------------------
Problem 3

Choice 1 wont actually add the friend to the file. Choice 3 and 4 don`t even work.

-------------------------------------------------------------------------------
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
friend.h

#include<iostream>
#include<string>
#include "date.h"
#ifndef FRIEND_H
#define FRIEND_H


class Friend{
    public:
	Friend();
	std::string get_name()const;
	Date get_bday()const;
	bool operator == (const Friend& other)const;
	bool operator != (const Friend& other)const;
	void input(std::istream& ins);
	void output(std::ostream& outs)const;
    private:
	std::string name;
	Date bday;
};

std::istream& operator >>(std::istream& ins,Friend& f);
std::ostream& operator <<(std::ostream& outs,const Friend& f);

#endif


friend.cc

#include "friend.h"
#include<sstream>

using namespace std;

Friend::Friend(){
name = "";
bday = Date();
}

std::string Friend::get_name()const{
	return name;
}

Date Friend::get_bday()const{
	return bday;
}
bool Friend::operator==(const Friend &f)const{
	return name == f.get_name() && bday == f.get_bday();
}

bool Friend::operator !=(const Friend &f)const{
	return !(*this == f);
}

void Friend::input(std::istream &ins){
	ins.ignore();
	getline(ins,name);
	ins.ignore();
	int d,m,y;
	string dt;
	getline(ins,dt);
	stringstream ss;
for(int i = 0;i<dt.length();i++){
  if(dt[i] == '/'){
   	ss<<" ";
  }else{
   	ss<<dt[i];
  }
}
ss>>d>>m>>y;
ss.clear();
bday = Date(d,m,y);
}

void Friend::output(std::ostream &ous)const{
	outs<<name<<"\t"<<bday;
}

std::ostream& operator<<(std::ostream &ous,const Friend &f){
	f.output(outs);
		return outs;
}

std::istream& operator>>(std::istream &ins,Friend &f){
	f.input(ins);
	return ins;
}
FBFriends.cc

#include "fbfriends.h"
#include <iostream>

FBFriends::FBFriends(){
   data = new Friend[5];
    used = 0;
    capacity = 5;
   current_index = -1;
}

FBFriends::~FBFriends(){
   delete[] data;
}

FBFriends::FBFriends(const FBFriends &other){
    *this = other;
}

void FBFriends::operator=(const FBFriends &other){
   if(this == &other){
       return;
}
    capacity = other.capacity;
    data = new Friend[capacity];
    used = other.used;
  for(int i = 0;i<used;i++){
    data[i] = other.data[i];
  }
}

void FBFriends::start(){
    current_index = 0;
}

void FBFriends::advance(){
    if(current_index < used-1)
       current_index++;
}

Friend FBFriends::current(){
    return data[current_index];
}

bool FBFriends::is_item(){
    return current_index > 0 && current_index < used;
}

void FBFriends::remove_current(){
if(is_item()){
  for(int i = current_index;i<used-1;i++){
   data[i] = data[i+1];
  }
  used--;
  }
}
void FBFriends::insert(const Friend &f){
   if(!is_item()){
     return;
	used++;
}
   if(used >= capacity){
      resize();
}
   for(int i = used;i>current_index;i--){
      data[i] = data[i-1];
}
      data[current_index] = f;
}

void FBFriends::attach(const Friend &f){
    if(used >= capacity){
       resize();
}
       data[used++] = f;
}

void FBFriends::show_all(std::ostream &outs)const{
    for(int i = 0;i<used;i++){
       outs<<data[i]<<' ';
}
}

Friend FBFriends::find_friend(const std::string &name)const{
    for(int i = 0;i<used;i++){
      if(data[i].get_name() == name){
        return data[i];
  }
 }
}

bool FBFriends::is_friend(const Friend &f)const{
   for(int i = 0;i<used;i++){
     if(f == data[i]){
        return true;
  }
}

        return false;
}

void FBFriends::load(std::istream &ins){
   Friend temp;
      while(!ins.eof()){
         while(ins>>temp){
              attach(temp);
  }
 }
}

void FBFriends::save(std::ostream &outs){
    for(int i = 0;i<used;i++){
       outs<<data[i]<<' ';
}
}

void FBFriends::bday_sort(){
  Friend temp;
      for(int i = 0;i<used;i++){
         for(int j = i+1;j<used;j++){
             if(data[i].get_bday() > data[j].get_bday()){
                 temp = data[i];
                 data[i] = data[j];
                 data[j] = temp;
   }
  }
}
}

void FBFriends::resize(){
    capacity = capacity + 5;
    Friend *temp = new Friend[capacity];
      for(int i = 0;i<used;i++){
          temp[i] = data[i];
}
    delete[] data;
    data = temp;
}

------------------------------------------------------------------------

fbfriends.h

#include<iostream>
#include<string>
#include<fstream>
#include "friend.h"
#ifndef FBFRIENDS_H
#define FBFRIENDS_H

class FBFriends{
    public:
	FBFriends();

	//The functions known as the Big 3
	~FBFriends();
	FBFriends(const FBFriends& other);
	void operator = (const FBFriends& other);

	// Functions for the internal iterator
	void start();
	void advance();
	bool is_item();
	Friend current();
	void remove_current();
	void insert(const Friend& f);
	void attach(const Friend& f);
	void show_all(std::ostream& outs)const;
	void bday_sort();
	Friend find_friend(const std::string& name)const;
	bool is_friend(const Friend& f) const;
	void load(std::istream& ins);
	void save(std::ostream& outs);
    private:
	void resize(); // increases the capacity of the container by 5
	Friend *data;
	int used;
	int capacity;
	int current_index;
};
#endif


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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include<iostream>
#include<fstream>
#include<cstdlib>
#include<string>
#include "friend.h"
#include "fbfriends.h"
using namespace std;

int menu();
int menu2();

int main(){

    Friend myfriend;
    FBFriends myfb;
    string friendname;
    int choice,choice2;
    string username, filename;
    ifstream fin;
    ofstream fout;
    bool cutout;

	cout<<"Welcome to Friends Management.\n\n";
	cout<<"Begin by entering your username: ";
	cout<<"test"<<endl;//*
	getline(cin,username);
	filename = username + ".txt";
	fin.open(filename.c_str());
    if(!fin.fail())
	myfb.load(fin);
	fin.close();
	choice = 0;
	choice2 = 0;
	cutout = false;
        FBFriends original(myfb);

	while(choice != 9){
	    choice = menu();
	    switch(choice){
		case 1:	cin>>myfriend;
		       	myfb.start();
			myfb.insert(myfriend);
			break;
		case 2: myfb.show_all(cout);
			break;
		case 3: {myfb.start();
			choice2 = 0;
			while(myfb.is_item()&& choice2 <= 5){
			    cout<<myfb.current();
			    choice2 = menu2();
			    if(choice2 == 1)
				myfb.remove_current();
			    else if(choice2 == 2){
				if(!cutout)
				cin>>myfriend;
				if(myfb.is_friend(myfriend)) 
				cout<<"Already in list.\n";
				else
				myfb.insert(myfriend);
				cutout = false;
			    }
			    else if(choice2 == 3){
				if(!cutout)
				cin >> myfriend;
                               if(myfb.is_friend(myfriend))
                                cout<<"Already in list.\n";
                                else
				myfb.attach(myfriend);
				cutout = false;
			    }
			    else if (choice2 == 4){
				myfriend = myfb.current();
				myfb.remove_current();
				cutout = true;
				}
			    else if(choice2 == 5){
				myfb.advance();
			    }
			    else
				cout<<"Going back to main menu.\n";
			}
			break;
			}
		case 4: myfb.bday_sort();
			break;
		case 5:{
			cout<<"Enter the name of your friend:\n";
			if(cin.peek() == '\n') cin.ignore();
			getline(cin, friendname);
			myfriend = myfb.find_friend(friendname);
			cout<<myfriend<<endl;
			break;
			}
		case 6:	original.show_all(cout);
			break;
		default: break;
		} // bottom of the switch
	} // bottom of the while
	fout.open(filename.c_str());
        if(!fout.fail())
	    myfb.save(fout);
	else
	    cout<<"Unable to save data.\n";
	fout.close();

	cout<<"Come visit your friends again soon.\n";
return 0;
}
	
int menu(){
	int ans;
	cout<<"Choose from the options below:"<<endl;
	cout<<"1 - Add a friend to the beginning of the list.\n";
	cout<<"2 - See all your friends.\n";
	cout<<"3 - Walk through the list, one friend at a time.\n";
	cout<<"4 - Sort your friends by birthday.\n";
	cout<<"5 - Find a friend so you can learn when they were born.\n";
	cout<<"6 - See the list you started with in today's session. \n";
	cout<<"9 - Leave the program.\n";
	cin>>ans;
    return ans;
}

int menu2(){
	int ans;
	cout<<"What would like to do with this friend?\n";
	cout<<"1 - Remove from the list.\n";
	cout<<"2 - Insert a new friend or cut-out friend before this friend.\n";
	cout<<"3 - Put a new friend or cut-out friend after this friend.\n";
	cout<<"4 - Cut this friend from the list to be inserted elsewhere.\n";
	cout<<"    If you want the friend earlier in the list you will need to start a new walk- 
        through.\n";
	cout<<"5 - Advance to the next friend.\n";
	cout<<"6 - Return to main menu.\n";
	cin>>ans;
    return ans;
}
Last edited on
Your spacing is all over the place. You need to go through and fix it. For instance, fixing the spacing in the following function uncovers a problem:
1
2
3
4
5
6
7
8
9
10
11
void FBFriends::insert(const Friend &f) {
    if (!is_item()) {
        return;
        used++;     /////// this will never happen ///////
    }
    if (used >= capacity)
        resize();
    for(int i = used; i > current_index; i--)
        data[i] = data[i-1];
    data[current_index] = f;
}


In your header files, the include guards should be at the very beginning of the file, before any of the includes.

That's just a couple of things. You need to go through the whole thing and fix the spacing (and any problems that uncovers) and then put all of the files as a zip file somewhere where we can download them. We shouldn't have to paste your program together into a bunch of separate files in order to try to run it.
Your input should look rather sth. like that:
1
2
3
4
5
6
7
8
void Friend::input(std::istream &ins)
{
  getline(ins,name);
  int d,m,y;
  char sep;
  ins >> d >> sep >> m >> sep >> y;
  bday = Date(d,m,y);
}
So, thomas I should get rid of ss, dt, and then what about my for loop? tpb what do you mean spacing? Why won`t my used work because its after my if statement? I can get a zip file in order.
What do you think I mean by spacing? I mean the spacing of your code. It's terrible. It's all over the place. And you've mixed tabs with spaces, too. You should just use spaces.

What do you mean why won't "my used work" if it's after an if statement. It's not after it. It's inside it, and there's a return before it.

Think, man. Jesus!

Here's the original spacing of FBFriends::insert. It's like a retard spaced it out.
1
2
3
4
5
6
7
8
9
10
11
12
13
void FBFriends::insert(const Friend &f){
   if(!is_item()){
     return;
	used++;
}
   if(used >= capacity){
      resize();
}
   for(int i = used;i>current_index;i--){
      data[i] = data[i-1];
}
      data[current_index] = f;
}

Last edited on
So you want it perfect with 2 spaces only? Like this.
1
2
3
4
5
6
7
8
9
10
11
12
13
void FBFriends::insert(const Friend &f){
  if(!is_item()){
    return;
    used++;
}
  if(used >= capacity){
    resize();
}
  for(int i = used;i>current_index;i--){
    data[i] = data[i-1];
}
  data[current_index] = f;
}

Last edited on
That's... better, but still weird. Whether you like 2x spaces, 4x spaces, or tabs is up to you. Just be consistent.
Your closing braces should be at the same text indentation level that your opening braces are.

Worst:
1
2
3
4
5
6
7
void Foo::fab() {
    if (a) {
        b;
            c++;
}
d;
}


Bad:
1
2
3
4
5
6
7
void Foo::fab() {
    if (a) {
        b;
        c++;
}
    d;
}


Good:
1
2
3
4
5
6
7
void Foo::fab() {
    if (a) {
        b;
        c++;
    }
    d;
}


Alternatively, also good (but keep consistent!):
1
2
3
4
5
6
7
8
9
void Foo::fab()
{
    if (a)
    {
        b;
        c++;
    }
    d;
}


The point remains that used++ is never incremented because you return right before it. Your use of indentation made this less noticeable and more error-prone.

_________________

PS: The following isn't a bug, but you should be more consistent. Why do you not use the getter method on the left-hand side, but you do use it on the right-hand side?
Either use the proper getter method on both sides, or neither.
1
2
3
bool Friend::operator==(const Friend &f)const{
	return name == f.get_name() && bday == f.get_bday();
}

name == f.name
or
get_name() == f.get_name()
Last edited on
Thanks man. Haven`t been coding very long I learn something new everyday.
Heres an example of how I like to indent and code. I find it easier to read and find error.
*Its a program that does nothing*

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
// Example of spacing program
#include <iostream>
#include <string>

int main()
{
  int first, second, third, fourth;
  first = 5;
  second = 4;
  third = 3;
  fourth = 666;
  while(first == 5)
  {
      if(second == 2)
      {
          //code will never execute unless "second" changes to 2
      }
      else
      {
          if(third == 3)
          {
              //notice indentation
              if(fourth == 666)
              {
                  //indent further
              }
          }
          else
          {
              //this code wont execute unless "third" is switched to something other than 3
          }
      }
  }
}
Whats the best way i could get a zip file to you guys?
Topic archived. No new replies allowed.