I have to write a program using loops. http://i.imgur.com/piDoY.jpg that is the assignment. He says we may use four seperate loops, each one to display the substring of a different length, and that we use string manipulation.
So far I have
#include <iostream>
#include <string>
using namespace std;
string myString;
int main()
{
cout << "Enter an arbitrary string:" << endl;
cin >> myString;
cout << "The sets of substrings of the given str " << myString << " are given below:" << endl;
endl;
cout << "The set of substrings of length 1 is:" << endl;
while(myString)
{
cout << myString[myString] << endl;
myString++
}
return 0;
}
Obviously this isn't right, I have looked online for tutorials and in our book for the class. I understand how to use for, while and do while loops but I am not sure how to display the things he wants. Someone please help
that does not work, because first: while(myString) : error: could not convert ‘myString’ to ‘bool’ myString[myString] : error: no match for ‘operator[]’ in ‘myString[myString]’ myString++ : no ‘operator++(int)’ declared for postfix ‘++’, and missing ; btw.
you need to declare a new variable to count.
1 2 3
for (size_t i = 0; i < myString.length(); i++) {
cout << myString[i] << endl;
}
now you have a working loop for the 1 letter parts. good luck with the others. if you dont get them working, just ask again.
Yes that works, would there be anyway you could explain this because I tried to edited it to make it display the two letter parts. I tried changing the i = 0 to a 1 just to see if it would work but i cant really figure it out. im kind of confused what each thing is doing
Yes it's actually due today. I have been trying to figure it out but can't. everything i try doesnt work or just does an infinite amount of whatever four letter word i type. so im not to sure how to do it
show me what you gotso far for the 2 letter substrings
or think of this: if you have a 4 letter word, ther are four 1 letter substrings, three 2 letter substrings two 3 letter substrings and one 4 letter substring.
so for the first loop you need from every position in the string the substring with the length 1 (this equals myString.at(i))
for the second one you go through the first three letters and get a substring from the actual position but length of 2.
for thethrid one you want the 3letter substrings from the first two letters.
and for the last output you just need the word, that is equal to a substring satrting at the beginning of the word, with the length of the word itself.