Hey everyone.

BKJ here, need some help with a homework, really appreciate some help.

Heres the question:
Write a program that reads one line of text, one character at a time, and then prints it with all its blanks, commas, astricts (*) and periods removed.

Heres my answer:

#include <iostream>
#include <cstring>
#include <string>
using namespace std;

void main()
{
	char a[80];
	cout << "Input a line of text, and the program shall print the line with \nthe blanks, commas,";
	cout << " asterixs and periods removed: " << endl;
	int i = 0;
	cin.get(a[i]);
	while(a[i] != '\n'|| i != 80)
	{
		cin.get(a[i]);
		i++;
	}
	i = 0;
	cout << "You entered: " << endl;
	while(a[i] != '\0')
	{
		if(a[i] != ' ' || a[i] != ',' || a[i] != '*' || a[i] != '.')
		{
			cout.put(a[i]);
		}
		else
		{
			cout.put(a[i]);
		}
		if(a[i] == '\0')
		{
			break;
		}
	}
	cout << endl;
}

It never stops inputting characters, just keeps on going, then it suddenly closes
Using Microsoft Visual C++ Express Edition 2008

Recommended Answers

All 4 Replies

Look very carefully at line 13:

while(a[i] != '\n'|| i != 80)

Think about the truth table this statement generates.

I can't seem to find what's wrong with line 13, although I do acknowledge that the problem lies there.
At one point, I did try putting this in the while loop:

if(a[i] == '\n')
{
     break;
}

But that caused infinite output.

I also tried this:

while(cin.get() != EOF)
{...}

Didn't work either.

I know that the concept of the program is simple and it would be a lot easier if my Professor didn't restrict the clast to cin.get() and cin.put(); could have used cin.getline().

Heres the solution I have up till now:

#include <iostream>
#include <cstring>
#include <string>
using namespace std;

void main()
{
	char a[80];
	cout << "Input a line of text, and the program shall print the line with \nthe blanks, commas,";
	cout << " asterixs and periods removed: " << endl;
	int i = 0;
	cin.get(a[i]);
	while(i != 80)
	{
		cin.get(a[i]);
		if(a[i] == '\n')
		{
			break;
		}
		i++;
	}
        //So the rest of the array doesn't get printed out
	while(a[i] != '\0' && i != 80)
	{
		a[i] = NULL;
		i++;
	}
	int j = 0;
	cout << "You entered: " << endl;
	while(a[j] != '\0')
	{
		if(a[j] != ' ' && a[j] != ',' && a[j] != '*' && a[j] != '.')
		{
			cout.put(a[j]);
		}
		else
		{
			if(a[j] == '\0')
			{
				break;
			}
		}
		j++;
	}
	cout << endl;
}

Here the problem:
The first character is read as a blank... WHY!?

Finally, solved. Complete code with comments:

#include <iostream>
#include <cstring>
#include <string>
using namespace std;

void main()
{
	//Declare array
	char a[80];
	cout << "Input a line of text, and the program shall print the line with \nthe blanks, commas,";
	cout << " asterixs and periods removed: " << endl;
	int i = 0;
	//Add while condition
	while(i != 80)
	{
		cin.get(a[i]);
		//Add new line condition
		if(a[i] == '\n')
		{
			break;
		}
		i++;
	}
	//So the rest of the array doesn't get printed out
	while(a[i] != '\0' && i != 80)
	{
		a[i] = NULL;
		i++;
	}
	int	j = 0;
	cout << "You entered: " << endl;
	//Add while condition
	while(a[j] != '\0')
	{
		if(a[j] != ' ' && a[j] != ',' && a[j] != '*' && a[j] != '.')
		{
			cout.put(a[j]);
		}
		j++;
	}
	cout << endl;
}

Big Thanks to WaltP for the help. The "line 13 - truth table" hint helped clear it up.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.