I dont know why this doesnt work, basically I want to calcualate the letters and numbers and white spaces and so on, nothing fancy. And it does work, it does calculate everything. However I seem to be stuck in the while loop or something because I cant print it out, for example the "ch" as im trying to do in the code.

#include <iostream>
#include <iomanip>
#include <string>
#include <cctype>


int main(void)
{
	char bokstav;
	int ch=0;
	int tal=0;
	int punkt=0;
	int space=0;
	int others=0;

	std::cout << "Type a sentence:";

	while (std::cin.get(bokstav))
	{
		if (isalpha(bokstav))
			ch++;
		else if (isdigit(bokstav))
			tal++;
		else if (ispunct(bokstav))
			punkt++;
		else if (isspace(bokstav))
			space++;
		else 
			others++;

                                //test
		std::cout << ch;
	
	}
	std::cout << ch;

}

I'm not sure what you mean, but I'd guess that you don't know how to signal end-of-file from the command line, so the loop never ends. Hold down the ctrl key and press z (ctrl+z) if you're on Windows and d (ctrl+d) if you're on Unix or Linux.

well I cant do anything after the while loop, it seems "stuck". Im trying to display on row 35 how many letters there were in the sentence but it doesnt work. as a matter a fact I cant do anything after the loop not even typing some texts whit cout,.

Hi, I edited some of your code. Here it is. The reason it won't come out from while loop is because isspace() included the newline ("\n") as a kind of "space". See this.

Anyway, it's a good practice to put return 0 at the end of int main(). :)

#include <iostream>
#include <iomanip>
#include <string>
#include <cctype>
using namespace std;

int main(void)
{
	char bokstav;
	int ch=0;
	int tal=0;
	int punkt=0;
	int space=0;
	int others=0;

	cout << "Type a sentence:";

	while (cin.get(bokstav) != "\n")
	{
		if (isalpha(bokstav))
			ch++;
		else if (isdigit(bokstav))
			tal++;
		else if (ispunct(bokstav))
			punkt++;
		else if (isspace(bokstav) && bokstav != '\n')
			space++;
		else 
			break;

		cout << "Inside\t\t:\t" << punkt << endl;
	}
	

	cout << "Outside\t\t:\t" << punkt << endl;
	
	return 0;
}

The code above didn't work for me, but I tweaked it a bit and it worked then try this:

#include <iostream>
#include <iomanip>
#include <string>
#include <cctype>
using namespace std;

int main()
{
	char bokstav;
	int ch=0;
	int tal=0;
	int punkt=0;
	int space=0;
	int others=0;

	cout << "Type a sentence:";
	cin >> bokstav;

	while (bokstav != "\n")
	{
		if (isalpha(bokstav))
			ch++;
		else if (isdigit(bokstav))
			tal++;
		else if (ispunct(bokstav))
			punkt++;
		else if (isspace(bokstav) && bokstav != "\n")
			space++;
		else 
			break;

		cout << "Inside\t\t:\t" << punkt << endl;
	}
	

	cout << "Outside\t\t:\t" << punkt << endl;
	
	return 0;
}

Note: this is just a tweak of raul15791's code. Full credit to him.

I would say the way to stop a loop is to have a way to break out of the loop built into the loop or have a way for the conditional of the while() to evaluate to false. The return value of cin.get() is an istream reference according to my reference literature. How an istream refernce compares to false I don't know, but I suspect it doesn't evaluate to false based on how you report the outcome of your code.

Input in C++ is routinely buffered before it is entered into a variable. Therefore, if I was to do this type of analysis of input I might do this:

string bokstav;
cout << "enter a string of characters. you may enter any valid key.  To stop entry press the enter key" << '\n';

get(bokstav);  

int i = 0;
while(stringInput[i] != '\0')
{
   //do the evaluation of each stringInput[i] here
   
   //advance to index of next char here
   ++i;
}

If I wanted to use a single char as input then I would provide a way out of the loop using an explicit break statement.

char ch;
while(1)
{
   cin.get()ch;
   //evaluate value of ch here

   //include a statement something like this
   if(ch == '\n')
      break;
}

I've never tried the input of ctrl+z to see if it would break out of the loop or not without having an explicit break statement.

>>while (bokstav != "\n")

bostav is a char and "\n" is a string, so you shouldn't be able to compare the two.

ty guys, you helped me alot!

You're welcome. Btw mark this thread solved...ty :D

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.