18 Replies - 282 Views - Last Post: 14 February 2013 - 02:13 AM
#1
loop tracking
Posted 13 February 2013 - 02:30 AM
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 .
Replies To: loop tracking
#2
Re: loop tracking
Posted 13 February 2013 - 02:32 AM
#3
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
#4
Re: loop tracking
Posted 13 February 2013 - 02:42 AM
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
#5
Re: loop tracking
Posted 13 February 2013 - 02:50 AM
raghav.naganathan, on 13 February 2013 - 02:42 AM, said:
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?
#6
Re: loop tracking
Posted 13 February 2013 - 03:15 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') // 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
#7
Re: loop tracking
Posted 13 February 2013 - 03:21 AM
lovesf, on 13 February 2013 - 03:20 PM, said:
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
#8
Re: loop tracking
Posted 13 February 2013 - 03:29 AM
TheKirk, on 13 February 2013 - 03:02 PM, said:
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
#9
Re: loop tracking
Posted 13 February 2013 - 03:47 AM
raghav.naganathan, on 13 February 2013 - 03:29 AM, said:
TheKirk, on 13 February 2013 - 03:02 PM, said:
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
#10
Re: loop tracking
Posted 13 February 2013 - 09:21 AM
TheKirk, on 13 February 2013 - 03:15 AM, said:
#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'.
#11
Re: loop tracking
Posted 13 February 2013 - 10:14 AM
#12
Re: loop tracking
Posted 13 February 2013 - 01:27 PM
#13
Re: loop tracking
Posted 13 February 2013 - 01:31 PM
#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.
#14
Re: loop tracking
Posted 13 February 2013 - 01:37 PM
#15
Re: loop tracking
Posted 13 February 2013 - 01:40 PM
Also, remember to put your code in complete code tags.
This post has been edited by Skydiver: 13 February 2013 - 01:40 PM
|
|

New Topic/Question
Reply



MultiQuote



|