program exits after fgets()?

Hi guys my program seems to exit pretty much terminates after I enter the username I type in Adam,the program stalls for a while then just terminates,


any idea why?

thanks

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
  #include <iostream>
#include <cstring>
#include <stdio.h>

using namespace std;

class myString{

  public:
      char* str;
      size_t length;

      myString(){}

      myString(char* s){

          length = strlen(s)+1;
          str = new char[length];
          memcpy(str,s,length);
      }
      myString(const myString &other){

          length = other.length;
          str = new char[length];
          memcpy(str,other.str,length);
      }

      myString(myString&& other){

          length = other.length;
          str = other.str;
          other.str = nullptr;
      }

      myString& operator=(myString other){

           swap(*this,other);
           return *this;

      }

      void swap(myString &a,myString &b){

          using std::swap;
          swap(a.length,b.length);
          swap(a.str,b.str);

      }
      int size(){

         return length;
      }
};

class User{

  public:
      myString userName;
      myString pass;

      User(){

      }

      User(myString u,myString p)
        : userName(u),pass(p)
      {

      }
};

class Node{

  public:

   Node* next;
   Node* previous;
   User user;

   Node(Node* n,Node* p,User u){

      next = n;
      previous = p;
      user = u;
   }

   void print(){

      cout << "username = " << user.userName.str << endl;
   }

};

class linkedList{

    public:

  Node* head = nullptr;
  Node* tail = nullptr;

  LinkedList(){

  }

  void addNode(User us){

       Node* node = new Node(head,nullptr,us);
       if(head != nullptr){

          head->previous = node;
       }
       head = node;

       if(tail == nullptr){

           tail = node;
       }
  }
  void printList(){

      Node* current = head;

      while(current != nullptr){

        current->print();

        current = current->next;
      }
  }

  void printReverse(){

      Node* current = tail;

      while(current != nullptr){

         current->print();
         current = current->previous;
      }
  }
};


int driver(){

    int choice;
    cout << "press one to create user,press two to view the list" << endl;

    linkedList list;

    while(true)
    {
        cin >> choice;

        switch(choice)
        {
        case 1:
        {
            char* user = "";
            char* pass = "";
            cin.get();
            cout << "enter username" << endl;
            fgets(user, sizeof(user), stdin);
            cout << "enter password" << endl;
            fgets(pass, sizeof(pass), stdin);

            myString userName(user);
            myString passWord(pass);
            list.addNode(User(userName,passWord));
        }
        break;
        case 2:
            list.printList();
            break;
        case 3:
            return 1;
        }
    }
    return 1;
}

int main()
{
   driver();
}
user and pass points to read-only memory that holds the string "". If you really want to use fgets you probably want them to be arrays of sufficient size instead.

1
2
3
4
5
6
char user[100];
char pass[100];
cout << "enter username" << endl;
fgets(user, sizeof(user), stdin);
cout << "enter password" << endl;
fgets(pass, sizeof(pass), stdin);
Why are you using fgets() instead of getline? How are you handling the new line character that may be part of the string?

using fgets instead of getline because I'm implementing my own string(note only doing it for some practice) I don't think getline will work with a c style string?

user and pass points to read-only memory that holds the string "".


really? but I didn't use the const keyword before them? I thought that if I used const before them use it would go into read only memory?

thanks
I don't think getline will work with a c style string?

cin has a getline member function that you can use.

 
cin.getline(user, sizeof(user));

http://www.cplusplus.com/reference/istream/istream/getline/


I thought that if I used const before them use it would go into read only memory?

Changing the pointer type will not affect what's being pointed to. String literals always gives you a read-only array of characters. Assigning a string literal to a non-const char* is no longer allowed since C++11 for this reason.

 
char* user = ""; // warning: ISO C++ forbids converting a string constant to ‘char*’ 
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

char user[1000];
            char pass[1000];
            cin.get();
            cout << "enter username" << endl;
            cin.getline(user,1000);
            //fgets(user, sizeof(user), stdin);
            cout << "enter password" << endl;
            //fgets(pass, sizeof(pass), stdin);
            cin.getline(pass,1000);

            myString userName(user);
            myString passWord(pass);
            list.addNode(User(userName,passWord));


the above code now works,but I had my doubts it would work as str in myString class is a char * where as the c style strings in my switch statement are char arrays how come the conversion works?

I know at the end of the day when we pass an array into a function it is pretty much treated as a pointer,but in the String class we are setting str = to a char array right?




Changing the pointer type will not affect what's being pointed to. String literals always gives you a read-only array of characters. Assigning a string literal to a non-const char* is no longer allowed since C++11 for this reason.


wow that's interesting I never knew that.


thanks

I had my doubts it would work as str in myString class is a char * where as the c style strings in my switch statement are char arrays how come the conversion works?

Arrays decay to pointers to the first element in the array very easily. It's just one of these rules that C++ inherited from C. The rules are intentionally designed to allow you to treat arrays and pointers to the first element of an array the same way with the same syntax (although there exists some corner cases where the difference is important, e.g. when using sizeof).

in the String class we are setting str = to a char array right?
str is a pointer to char.
new char[length] creates a char array and returns a pointer to the first element of that array.
After str = new char[length]; in the constructor str will point to the first element of the newly created array.
Last edited on
Topic archived. No new replies allowed.