Debugging

Hi, I have written a simple C++ program to read a string and output only even words. For example, in the first sentence, the output should be "I written simple program read string output even." It's executing but it's not taking input correctly. Even after I enter a string and hit enter, it just stops there and doesn't do anything. Here it is:

#include<iostream.h>
#include<iomanip.h>
#include<conio.h>
#include<string.h>
void main()
{
char s[100],t[100][100];
int i,j;
clrscr();
cout<<"Enter the string: ";
cin.getline(s,100);
for(i=0;i<100;i++)
for(j=0;s[j]!=char(32);j++)
t[i][j]=s[j];
for(i=1;i<100;i+2)
{
for(j=0;j<100;j++)
s[j]=t[i][j];
s[j]=char(32);
}
cout<<"The new string is: ";
for(i=0;i<100;i++)
cout<<s[i];
cout<<endl;
getch();
}

Check if something needs to be corrected. Please keep it simple as I'm a beginner. There may be a better program using complicated things, but try to modify the above program itself.
' ' is space. char(32) is harder to read, not everyone memorized the ascii table.

void main isnt legal. main shall be of type int, and shall return 0 upon successful completion.

<iostream>, <iomanip>, <cstring> are the modern headers, unsure about conio

i think you want i+= 2 in the for loop

And I think that last one is your actual error. It would loop forever because i never changes.

Your code looks like what i grew up on ... you want to learn the c++ type string sooner rather than later. Are you using a 30 year old book?

Last edited on
The code bears all the signs of Turbo C++ which is an obsolete program originally for DOS, then later for Windows 3.1.

I'm aware that it is still used in some educational courses, but aside from that it should not be used at all. Some modern (or reasonably so) C++compilers/IDEs are covered at the start of the tutorial pages:
http://www.cplusplus.com/doc/tutorial/introduction/
- Read the warnings
|| foo.cpp: In function ‘int main()’:
foo.cpp|15 col 24| warning: for increment expression has no effect [-Wunused-value]
||   for(i = 1; i < 100; i + 2) {
||                       ~~^~~

- Use a sanitizer
foo.cpp:18:6: runtime error: index 100 out of bounds for type 'char [100]'
foo.cpp:18:8: runtime error: store to address 0x7fffffffe064 with insufficient space for an object of type 'char'
1
2
3
		for(j = 0; j < 100; j++) //this loop ends when j=100
			s[j] = t[i][j];
		s[j] = char(32);  //here j is 100  
`s' was declared as char s[100], so 100 is not a valid index (the greatest valid index is 99)

- Use the debugger (comments between brackets)
$ gdb a.out
(gdb) run
Enter the string: oh brave new world
[takes too long, pressed <C-c>]
Program received signal SIGINT, Interrupt.
0x00005555555556a7 in main () at foo.cpp:17
(gdb) list
15		for(i = 1; i < 100; i + 2) {
16			for(j = 0; j < 100; j++)
17				s[j] = t[i][j];
18			s[j] = char(32);
19		}
(gdb) print j
$1 = 27
(gdb) print i
$2 = 1
then you may continue step by step and notice that `i' doesn't change its value.


Also, you need to review your algorithm, you are overwritting `s' on lines 16--17
Last edited on
I tried with ' '. It didn't work. So, I used that. Anyway, this version of c++ was recommended for a beginner like me by my friend. Yeah, I guess it's obsolete. And yeah, that's turbo C++, I'll try a different one now. Thanks!
Topic archived. No new replies allowed.