Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Counting whitespaces in a string

$
0
0
I'm struggling to figure out how to get a program to read entered text and count the characters. Objective (overall) is to take the number of characters and multiply it by an entered value to "calculate" the cost per letter for a web banner (or something similar). Yes, it is homework, and I'm not asking for anyone to complete the assignment for me, just point me in a direction on how to finish a section of the program. I am not able to use the length or size methods (so nothing like phrase.size() can be used.

Here is what I have.
cout << "Welcome to the Slogan Expenditure 3542" << endl;
cout << "**************************************" << endl;
cout << "Please enter in the slogan to be priced: " << endl;
cin >> adPhrase;
cout << "Please enter the expected cost per letter. " << endl;
cin >> costPPL;
cout << endl;



for(i=0;adPhrase[i]!='\0';i++){  
	if (adPhrase[i] == ' '){
		i++;
	}
} 
	
	
cout << "Your slogan has " << i << " letters in it"<< endl;
cout << endl;
cout << "This will cost approximately $"<< i*costPPL << " total" << endl;




The for loop is to read the text entered by the user, it reads the first word just fine, but stops at the first word. . I thought that if I used an if statement to account for the spaces it would move on in to the next word, but that does not appear to be the case. I'm lost, and the only help I've received is to use NULL, but there's nothing in the material for the assignment that tells me how or even where to use it. I can't help but feel that I'm close to where I need to be...just not quite there.

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>