Counting whitespaces in a string

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 588 Views - Last Post: 04 February 2013 - 06:57 AM Rate Topic: -----

#1 SplinteredChaos  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 112
  • Joined: 15-September 11

Counting whitespaces in a string

Posted 01 February 2013 - 10:42 PM

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.

Is This A Good Question/Topic? 0
  • +

Replies To: Counting whitespaces in a string

#2 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: Counting whitespaces in a string

Posted 02 February 2013 - 12:58 AM

In your for loop, you are using i as the iterator. Your are ALSO using i as a conditional counter. WTF?

In your for loop, you are using i as the iterator. Your are ALSO using i as a conditional counter. WTF?


In your for loop, you are using i as the iterator. Your are ALSO using i as a conditional counter. WTF?

Work it out with paper - question "What happens when you print up adphrase[i]? inside this loop? What letters will be skipped?

Yes, those first 3 questions were a bit of theater to get you thinking full blast.
Was This Post Helpful? 0
  • +
  • -

#3 SplinteredChaos  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 112
  • Joined: 15-September 11

Re: Counting whitespaces in a string

Posted 02 February 2013 - 01:43 AM

Hoping that you'll avoid getting frustrated with my ignorance I am still very much learning terms and concepts at this stage. So bare with me.

I was under the impression that i is to move me through the string (while in the for loop), representing the position as it is reading the string. The number would increase as it got deeper within the string. So an entered value of Hello would ultimately make i = 5. The program stops at the whitespace. If I enter Hello World, the program is only reading hello, and gives me the value off of that.

I think that the '\0' terminating sequence is an area I'm failing things in (I may be reaching there). My reasoning for thinking that is if I put underscores between the words it works fine. It counts the characters entered and performs the calculations. I believe an if statement could be used to track the underscores and remove them from the mathematical calculation to happen later in the program if I did not want the user to realize a charge from those characters.

To answer your last question, when I print adPhrase[i] nothing prints. Is this because the i variable is only available during the for loop?
Was This Post Helpful? 0
  • +
  • -

#4 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: Counting whitespaces in a string

Posted 02 February 2013 - 02:23 AM

You can't use a loop counter (an iterator), as a conditional counter, as well. It's like using a marble to replace a car wheel. They both are round, but they're not the same. That's how the logic is here - different.

Print out your adphrase after it's entered. You're on the right track, but not quite there.
Was This Post Helpful? 0
  • +
  • -

#5 SplinteredChaos  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 112
  • Joined: 15-September 11

Re: Counting whitespaces in a string

Posted 02 February 2013 - 02:37 AM

So am I correct in understanding that the primary issue is with the for loop.
for(i=0;adPhrase[i]!='\0';i++){ 

    if (adPhrase[i] == ' '){
        i++;
    }
}



In particular the
(i=0;adPhrase[i]!='\0';i++)
so I cannot use the i at the beginning and then also in the adPhrase[]? or am I confusing things?

Trying to make sure I am putting my terminology in the right spots, the loop counter is the i=0; and the conditional counter is adPhrase[i}?
Was This Post Helpful? 0
  • +
  • -

#6 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: Counting whitespaces in a string

Posted 02 February 2013 - 02:40 AM

Print out adphrase, after you enter it, and before you do anything else.

Yes, the i counter and conditional is a problem, but it's not the first problem.
Was This Post Helpful? 1
  • +
  • -

#7 SplinteredChaos  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 112
  • Joined: 15-September 11

Re: Counting whitespaces in a string

Posted 02 February 2013 - 02:51 AM

Not the first problem or not the only problem?

When you say print out adPhrase after it is entered
cin >> adPhrase;
, are you meaning to
cout >> adPhrase;
?

This post has been edited by SplinteredChaos: 02 February 2013 - 02:52 AM

Was This Post Helpful? 0
  • +
  • -

#8 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: Counting whitespaces in a string

Posted 02 February 2013 - 03:04 AM

Both. Will you be printing out adPhrase, sometime before the next Ice Age? ;)
Was This Post Helpful? 0
  • +
  • -

#9 SplinteredChaos  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 112
  • Joined: 15-September 11

Re: Counting whitespaces in a string

Posted 02 February 2013 - 10:31 AM

The purpose of the program doesn't really require me to print adPhrase after it is collected. I really just need it to hold a string and count the length of the string (simulated with for loop not length method). Once the length is calculated I take that value and multiply it by something else collected from the user.
Was This Post Helpful? 0
  • +
  • -

#10 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1336
  • View blog
  • Posts: 4,577
  • Joined: 19-February 09

Re: Counting whitespaces in a string

Posted 02 February 2013 - 12:37 PM

for(i=0;adPhrase[i]!='\0';i++){ 

    if (adPhrase[i] == ' '){
        i++;
    }
}



If the input is something simple such as "a b" then i will iterate until it is 3, also equal to the length. By your logic the number of spaces will be equal to i which is 3.
Was This Post Helpful? 0
  • +
  • -

#11 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: Counting whitespaces in a string

Posted 02 February 2013 - 02:13 PM

There is nothing more useful in debugging, than simply printing out a dodgy variable's value.
Was This Post Helpful? 0
  • +
  • -

#12 SplinteredChaos  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 112
  • Joined: 15-September 11

Re: Counting whitespaces in a string

Posted 02 February 2013 - 06:54 PM

View Post#define, on 02 February 2013 - 12:37 PM, said:

for(i=0;adPhrase[i]!='\0';i++){ 

    if (adPhrase[i] == ' '){
        i++;
    }
}



If the input is something simple such as "a b" then i will iterate until it is 3, also equal to the length. By your logic the number of spaces will be equal to i which is 3.


Got a feeling it'll end up being a dumb question, but what do you mean [/i], if we were to apply numbers to that would it be 3*3 = 9/3 = 3? If so that is exactly what I'm looking for. However, the program is only reading the first character, so 'a'. The value of [i] = 1.
Was This Post Helpful? 0
  • +
  • -

#13 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1336
  • View blog
  • Posts: 4,577
  • Joined: 19-February 09

Re: Counting whitespaces in a string

Posted 02 February 2013 - 08:08 PM

How are you declaring/creating adPhrase?

Ahh, this will only read up to a whitespace character so a word in effect.

  cin >> adPhrase;



You will need to use getline to get a phrase.

.

This post has been edited by #define: 02 February 2013 - 08:15 PM

Was This Post Helpful? 1
  • +
  • -

#14 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 770
  • View blog
  • Posts: 2,250
  • Joined: 20-March 10

Re: Counting whitespaces in a string

Posted 03 February 2013 - 11:27 AM

View Post#define, on 03 February 2013 - 03:08 AM, said:

You will need to use getline to get a phrase.

.


Yay you got there..

+1

#define of course means getline(cin,adphrase);

Snoopy

This post has been edited by snoopy11: 03 February 2013 - 11:28 AM

Was This Post Helpful? 0
  • +
  • -

#15 SplinteredChaos  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 112
  • Joined: 15-September 11

Re: Counting whitespaces in a string

Posted 03 February 2013 - 03:08 PM

 string adPhrase;
is how I have adPhrase declared at this point. Is that not correct?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2