Invalid pointer with string

Hi,
I have a problem with this program.
The goal of this assignment is to perform rot13 on a string.
The math works fine, and the output is as expected.
After the program converts all of the text, it crashes with an "invalid pointer" error

I think it has something to do with the string?...




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
#include <iostream>
#include <string>

using namespace std;
using std::string;



char encodeDecode(char);

int main()
	{
	
	string inputString;
	char   outputChar;
	
	cout << "Please enter text to be decoded/encoded:  ";
	cin >> inputString;
	
	cout << "Decoded/encoded message                :  ";

	int i;
	for (i = 0 ; i < inputString.size() ; i++)
		{
		outputChar = encodeDecode(inputString[i]);
		cout << outputChar;
		};
	
	cout << endl << "Done" << endl;
	string anyKey = 0;
	getline (cin , anyKey);
	return 0;
	}
	
	
	char encodeDecode(char inputChar)
	{
	
	char outputChar;

	if (isupper(inputChar))
			outputChar = ((inputChar - 65 + 13 ) % 26 + 65);
			
	if (islower(inputChar))
			outputChar = ((inputChar - 97 + 13 ) % 26 + 97);
	
	if (islower(inputChar) == isupper(inputChar))
			outputChar = inputChar;
	
	return outputChar;
	
	};

string anyKey = 0;

std::strings don't like having zero assigned to them. They tend to throw exceptions :)
use string anyKey;

remove the assigment to zero.
....

Shot myself in the foot. Thanks!
I'm on a gdb promotion kick; if you'd run this under gdb, you'd have seen something like the following:


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
(gdb) run
Starting program: /home/j/badCode/a.out 
terminate called after throwing an instance of 'std::logic_error'
  what():  basic_string::_S_construct NULL not valid

Program received signal SIGABRT, Aborted.
0x00007ffff72dfa75 in *__GI_raise (sig=<value optimised out>) at ../nptl/sysdeps/unix/sysv/linux/raise.c:64
64	../nptl/sysdeps/unix/sysv/linux/raise.c: No such file or directory.
	in ../nptl/sysdeps/unix/sysv/linux/raise.c
(gdb) bt
#0  0x00007ffff72dfa75 in *__GI_raise (sig=<value optimised out>) at ../nptl/sysdeps/unix/sysv/linux/raise.c:64
#1  0x00007ffff72e35c0 in *__GI_abort () at abort.c:92
#2  0x00007ffff7b958e5 in __gnu_cxx::__verbose_terminate_handler() () from /usr/lib/libstdc++.so.6
#3  0x00007ffff7b93d16 in ?? () from /usr/lib/libstdc++.so.6
#4  0x00007ffff7b93d43 in std::terminate() () from /usr/lib/libstdc++.so.6
#5  0x00007ffff7b93e3e in __cxa_throw () from /usr/lib/libstdc++.so.6
#6  0x00007ffff7b2f9a7 in std::__throw_logic_error(char const*) () from /usr/lib/libstdc++.so.6
#7  0x00007ffff7b71931 in ?? () from /usr/lib/libstdc++.so.6
#8  0x00007ffff7b71a53 in std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&) () from /usr/lib/libstdc++.so.6
#9  0x0000000000400e60 in main () at badcode14.cpp:13
(gdb) up
#1  0x00007ffff72e35c0 in *__GI_abort () at abort.c:92
92	abort.c: No such file or directory.
	in abort.c
(gdb) up
#2  0x00007ffff7b958e5 in __gnu_cxx::__verbose_terminate_handler() () from /usr/lib/libstdc++.so.6
(gdb) up
#3  0x00007ffff7b93d16 in ?? () from /usr/lib/libstdc++.so.6
(gdb) up
#4  0x00007ffff7b93d43 in std::terminate() () from /usr/lib/libstdc++.so.6
(gdb) up
#5  0x00007ffff7b93e3e in __cxa_throw () from /usr/lib/libstdc++.so.6
(gdb) up
#6  0x00007ffff7b2f9a7 in std::__throw_logic_error(char const*) () from /usr/lib/libstdc++.so.6
(gdb) up
#7  0x00007ffff7b71931 in ?? () from /usr/lib/libstdc++.so.6
(gdb) up
#8  0x00007ffff7b71a53 in std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&) () from /usr/lib/libstdc++.so.6
(gdb) up
#9  0x0000000000400e60 in main () at badcode14.cpp:13
13	string z = 0;



and it would all have been clear. I thoroughly recommend gdb or another similar debugger.
Last edited on
Adding that to the armory!
Moschops
Fafner

Thank You!
You problem is

string anyKey = 0;

use

char anyKey = 0;
Topic archived. No new replies allowed.