loop tracking

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 553 Views - Last Post: 14 February 2013 - 02:13 AM Rate Topic: -----

#1 lovesf  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 13-February 13

loop tracking

Posted 13 February 2013 - 02:30 AM

using for loop to print if the word contains 'y' , and
only print the world contains 'y'. i a confused how to make sure
if prints only one time even though theword might have more than one y .
Is This A Good Question/Topic? 0
  • +

Replies To: loop tracking

#2 TheKirk  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 58
  • Joined: 10-February 13

Re: loop tracking

Posted 13 February 2013 - 02:32 AM

Your english is horrible. Could you show us some code to clarify?
Was This Post Helpful? 0
  • +
  • -

#3 lovesf  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 13-February 13

Re: loop tracking

Posted 13 February 2013 - 02:40 AM

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

int main() {
    string word;
    
    cout << "Please enter word ";
    cin >> word;
    
    for(unsigned int i=0; i< word.size(); i++) {
        
        if(word.at[i]=='y' | {
        
        cout << "Your word, " << word << "contains " << word.at(i)<< "\n";
    }
    }

 
   
   return 0;
   }

This post has been edited by JackOfAllTrades: 13 February 2013 - 04:24 AM
Reason for edit:: Added code tags

Was This Post Helpful? 0
  • +
  • -

#4 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: loop tracking

Posted 13 February 2013 - 02:42 AM

Well, you could probably do this.

Take the contents of the string as an array and iterate through the indexes using a for loop to see if any of the indexes contain the letter 'y'.

regards,
Raghav
Was This Post Helpful? 0
  • +
  • -

#5 lovesf  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 13-February 13

Re: loop tracking

Posted 13 February 2013 - 02:50 AM

View Postraghav.naganathan, on 13 February 2013 - 02:42 AM, said:

Well, you could probably do this.

Take the contents of the string as an array and iterate through the indexes using a for loop to see if any of the indexes contain the letter 'y'.

regards,
Raghav



Thanks for the reply but i still dont get it. are u telling me to create another for loop?
Was This Post Helpful? 0
  • +
  • -

#6 TheKirk  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 58
  • Joined: 10-February 13

Re: loop tracking

Posted 13 February 2013 - 03:15 AM

It was nothing more then a syntax errror:

#include <iostream>
#include <string>

using namespace std;

int main() 
{
	string word;
	cout << "Please enter word ";
	cin >> word;
	for(unsigned int i=0; i< word.size(); i++) 
	{
		if(word.at[i]=='y') // Instead of | this must be )
		{
			cout << "Your word, " << word << "contains " << word.at(i) << "\n";
		}
	}
	return 0;
}


That should do the trick.

This post has been edited by TheKirk: 13 February 2013 - 03:15 AM

Was This Post Helpful? 1
  • +
  • -

#7 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: loop tracking

Posted 13 February 2013 - 03:21 AM

View Postlovesf, on 13 February 2013 - 03:20 PM, said:

Thanks for the reply but i still dont get it. are u telling me to create another for loop?


Well, I'm sorry...I didn't notice your code...my page had not been refreshed.

TheKirk is right...you needed to change the | to a )

regards,
Raghav

This post has been edited by raghav.naganathan: 13 February 2013 - 03:22 AM

Was This Post Helpful? 0
  • +
  • -

#8 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: loop tracking

Posted 13 February 2013 - 03:29 AM

View PostTheKirk, on 13 February 2013 - 03:02 PM, said:

Your english is horrible. Could you show us some code to clarify?


Well, I would suggest that you reply more politely as not everyone here has English( that is with capital 'E') as their first language.

A 'please' could also be included like 'Could you please show us some code to clarify?'...see the power of the 'please' in that question?

regards,
Raghav

This post has been edited by raghav.naganathan: 13 February 2013 - 03:30 AM

Was This Post Helpful? 0
  • +
  • -

#9 TheKirk  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 58
  • Joined: 10-February 13

Re: loop tracking

Posted 13 February 2013 - 03:47 AM

View Postraghav.naganathan, on 13 February 2013 - 03:29 AM, said:

View PostTheKirk, on 13 February 2013 - 03:02 PM, said:

Your english is horrible. Could you show us some code to clarify?


Well, I would suggest that you reply more politely as not everyone here has English( that is with capital 'E') as their first language.

A 'please' could also be included like 'Could you please show us some code to clarify?'...see the power of the 'please' in that question?

regards,
Raghav


The fact is that it's hard to understand somebody's question if it's poorly written. That goes for any language. This is an English forum so I would expect English. See the "Do's" when 'Asking a question':

Quote

  • Use proper English and spelling


He did not. Also, of course not everybody is native but that doesn't mean we can't expect them to write proper english.

This post has been edited by TheKirk: 13 February 2013 - 03:48 AM

Was This Post Helpful? 2
  • +
  • -

#10 lovesf  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 13-February 13

Re: loop tracking

Posted 13 February 2013 - 09:21 AM

View PostTheKirk, on 13 February 2013 - 03:15 AM, said:

It was nothing more then a syntax errror:

#include <iostream>
#include <string>

using namespace std;

int main() 
{
	string word;
	cout << "Please enter word ";
	cin >> word;
	for(unsigned int i=0; i< word.size(); i++) 
	{
		if(word.at[i]=='y') // Instead of | this must be )
		{
			cout << "Your word, " << word << "contains " << word.at(i) << "\n";
		}


	}
	return 0;
}


That should do the trick.



Thank you for ur help. my question is how to make sure it only prints out " your word contains Y ", once, even if the word contains more than one 'y'.
Was This Post Helpful? 0
  • +
  • -

#11 lovesf  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 13-February 13

Re: loop tracking

Posted 13 February 2013 - 10:14 AM

Thank you for ur help. my question is how to make sure it only prints out " your word contains Y ", once, even if the word contains more than one 'y'.
Was This Post Helpful? 0
  • +
  • -

#12 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3589
  • View blog
  • Posts: 11,165
  • Joined: 05-May 12

Re: loop tracking

Posted 13 February 2013 - 01:27 PM

You could break out of the loop so that it only prints out once.
Was This Post Helpful? 1
  • +
  • -

#13 lovesf  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 13-February 13

Re: loop tracking

Posted 13 February 2013 - 01:31 PM

how can u print only once " your word contains y" EVEN IF THE WORD MIGHT HAVE MORE THAN one Y in the word. ex YAYEY




#include <iostream>
#include <string>

using namespace std;

int main() 
{
	string word;
	cout << "Please enter word ";
	cin >> word;
	for(unsigned int i=0; i< word.size(); i++) 
	{
		if(word.at[i]=='y') // Instead of | this must be )
		{
			cout << "Your word, " << word << "contains " << word.at(i) << "\n";


This post has been edited by Skydiver: 13 February 2013 - 01:40 PM
Reason for edit:: Fixed code tags. Remember to do this yourself.

Was This Post Helpful? 0
  • +
  • -

#14 lovesf  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 13-February 13

Re: loop tracking

Posted 13 February 2013 - 01:37 PM

please please some one explian it for me. i need it.
Was This Post Helpful? 0
  • +
  • -

#15 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3589
  • View blog
  • Posts: 11,165
  • Joined: 05-May 12

Re: loop tracking

Posted 13 February 2013 - 01:40 PM

Do not open new topics when it is about the same body of code.

Also, remember to put your code in complete code tags.

This post has been edited by Skydiver: 13 February 2013 - 01:40 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2