Hello, I am making a program in which I am supposed to create a class of string and then try to make functions of the string library.
So, I started off and made the following program (only one function has been defined until now)
#include <iostream>
usingnamespace std;
class string1 {
private:
char *s;
staticint c;
string1()
{
c++;
s = newchar[100];
getchar();
cout << "Enter String#" << c << ": ";
cin.getline(s, 100);
int a = 0;
for (int i = 0; s[i] != '\0'; i++)
a++;
char *temp = newchar[a];
for (int i = 0; i <= a; i++)
s[i] = temp[i];
delete[] s;
s = temp;
}
public:
static string1* create()
{
returnnew string1();
}
char charAt(int a)
{
return s[a];
}
};
string1 **p;
void create(int &);
int string1::c = 0;
int main()
{
int n = 0;
p = new string1*[n];
cout << "1) Add an Object" << endl
<< "2) Find Character at Specified Index" << endl
<< "Your Selection: ";
int choice;
bool fh = true;
while (fh)
{
cin >> choice;
switch (choice)
{
case 1:
create(n);
break;
case 2:
cout << "Enter Index: ";
cin >> choice;
cout << p[0]->charAt(choice);
break;
case 3:
fh = false;
break;
}
}
cout << endl;
system("pause");
return 0;
}
void create(int &n)
{
n++;
string1 **temp = new string1*[n];
for (int i = 0; i < n; i++)
temp[i] = p[i];
delete[]p;
p = temp;
p[n-1] = string1::create();
}
The problem that I am getting here is this that the program runs ok. It asks to select the option upon which I press "1" and then it also calls the Constructor (because the cout statement in Constructor executes which shows constructor is working)
However, now, once I enter a string... It just stops there and doesn't go ahead (whereas it should get out of constructor and come back to the main menu). It is apparently not leaving the constructor and I just can't seem to find the problem. Can anyone help me solve this, please?
Thank you very much!
(Here is a sample output.. Program not proceeding once a string is entered)
1 2 3 4
1) Add an Object
2) Find Character at Specified Index
Your Selection: 1
Enter String#1: Hello World
$ g++ -g foo.cpp
$ gdb -q ./a.out
Reading symbols from ./a.out...done.
(gdb) run
Starting program: /home/sc/Documents/a.out
1) Add an Object
2) Find Character at Specified Index
Your Selection: 1
Enter String#1: hello
^C
Program received signal SIGINT, Interrupt.
0x00007ffff756c260 in __read_nocancel () at ../sysdeps/unix/syscall-template.S:84
84 ../sysdeps/unix/syscall-template.S: No such file or directory.
(gdb) bt
#0 0x00007ffff756c260 in __read_nocancel () at ../sysdeps/unix/syscall-template.S:84
#1 0x00007ffff74ef5e8 in _IO_new_file_underflow (fp=0x7ffff78398e0 <_IO_2_1_stdin_>) at fileops.c:592
#2 0x00007ffff74f060e in __GI__IO_default_uflow (fp=0x7ffff78398e0 <_IO_2_1_stdin_>) at genops.c:413
#3 0x00007ffff74eb108 in _IO_getc (fp=0x7ffff78398e0 <_IO_2_1_stdin_>) at getc.c:38
#4 0x00007ffff7b3eb4d in __gnu_cxx::stdio_sync_filebuf<char, std::char_traits<char> >::underflow() () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#5 0x00007ffff7b4bb21 in std::istream::sentry::sentry(std::istream&, bool) () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#6 0x00007ffff7b4bd1e in std::istream::operator>>(int&) () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#7 0x0000000000400d76 in main () at foo.cpp:48
(gdb) frame 7
#7 0x0000000000400d76 in main () at foo.cpp:48
48 cin >> choice;
You're back in main, trying to read a choice.
But you're driving blind because the 'what to do' prompt is missing.