this game is called "pig latin" it spit all the string, put the first consonant to the end and adds "AY" to the end. for example --> "this is test" --->"histAY isAY estAY". it gives me string subscript out of range. thanks

#include <iostream>
#include "conio.h"
#include <string>
#include <sstream>

using namespace std;

int main(){
    string sentence,temp;
    int index;
    int a,i=0,sayac=0;
    string harf;

cout<<"enter the sentence"<<sentence <<endl;
getline(cin,sentence);
cin.clear();

cout<<"\nlenght: "<<sentence.length()<<endl;


temp=sentence;
for( i=0; i<sentence.length();i++)
    if(temp[i]==' '){
        sayac++;
        temp[i]='**';
    }
cout << "\n word count: " <<sayac+1<<endl;


//split
    istringstream iss(sentence);
    while(iss){
    {
        string sub;
        iss >> sub;
        cout << sub << endl;
        a=sub.length();

        index=sub.find_first_not_of("aeuio");
        cout << "first consonant:"<< sub[index]<<endl;
        harf=sub[index];
        sub.insert ( a, harf ) ;
        sub.insert (a+1, "AY"); 
        sub.erase(index,1);
        cout << "new word:" << sub << endl;

    }

//split son

    getch();
    return 0;
}

Recommended Answers

All 3 Replies

    while(iss){
    {
        string sub;
        iss >> sub;
        cout << sub << endl;
        a=sub.length();

        index=sub.find_first_not_of("aeuio");
        cout << "first consonant:"<< sub[index]<<endl;
        harf=sub[index];
        sub.insert ( a, harf ) ;
        sub.insert (a+1, "AY"); 
        sub.erase(index,1);
        cout << "new word:" << sub << endl;

    }

This section does not do what you want. Even if it works you get
"each" ==> "eahcay"

index=sub.find_first_not_of("aeuio"); What if index is zero?

The instructions "put the first consonant to the end" does not mean the fist consonant in the word. It means if the first letter is a consonant.
each becomes "eachway" (note -- add "way" not letter+"ay"
touch becomes "ouchtay"
Also note that "this" becomes "isthay" and "straight" becomes "aightstray". It's the first consonant sound(s), not the first consonant letter

You did good outputting the strings to see what is in them. Now output the integers to see why you are getting "subscript out of range."

Additionally, do you want to insert at the end? Or is there a better way without having to know the length?

Taking a quick look,

" index=sub.find_first_not_of("aeuio");
    cout << "first consonant:"<< sub[index]<<endl;
"

what happens when that find_first_not_of fails?

you are right its a wrong with my consonant way. it must be solved with if statement but the "out of range" error is more important to me now. i have to face with it.
index holds the firs consonant's string index. maybe im wrong. sorry for my bad English. thanks :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.