string usage - starting from end

Hey this is my first post. How is everyone doing? Im working on my first c++ program which probably is more advanced first program than what most people would start with. I need help getting it started. I have made it in php so I know what Im looking for. I need to take:



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
english:
    //
        int b;
        cout << "(1) " << endl;
        cout << "(2) " << endl;
        cout << "(3) " << endl;
        cout << endl << ": ";
        cin >> b;
    if (b == 1) {
        //
        string x;
        cout << " blah blah blah: ";
        getline(cin, x);       //< --- this is not the problem but if i use it like this then it gets, "cin b" and acts as if its been entered and proceeds to the next step.  Anyway to clear the "cin b" before it reaches "getline(cin, x)? (if its even used in this way) I know i could use "cin >> x;" but need to the whole line of text.

        cout << "blah blah blah: ";
        //insert more code -----> this is where the main question comes in....I need to take a word or string of words break them into an array, (which i have found because yessss "Google is my friend", and then insert, replace, etc. characters in the word starting from the end rather than from the beginning.....so lets say "hello" was entered.  I could change the second character from the end or if "tattoo" was entered, it would change maybe the 4th character from the end.  (words are just examples).
        cout << x << endl << endl;
        system("Pause");
        goto english;
    }
    else if (b == 2) {
        return;
    }
    else if (b == 3) {
        exit(1);
    }
    else {
        cout << "That is not an option";
        goto begin;
    }
}


oh, also when an option other than 1, 2, 3 is entered in, it goes through a loop that never quits until i stop the program. Any ideas? This is just part of code hopefully it will help you understand.
Last edited on
regarding your first question, use getline like this:

1
2
3
4
5
 
string x;
char someText[512];      // some safe side buffer.
cin.getline(&someText, 512);
x.assign(&someText, 512);     // If you are strict to use strings. 


second question is not clear, though just for some thought, to random walk through a string object you can use iterators.
string has both reverse and forward iterator.
or may be u can use functions like at(), operator[] or something.
use iterator like string::reverse_iterator .......
can you give more details of how you would use the first question answer. I tried it and came up errors


Error E2285 test.cpp 23: Could not find a match for 'istream::getline(char ( *)[512],int)' in function english()

Error E2285 test.cpp 24: Could not find a match for 'string::assign(char ( *)[512],int)' in function english()

Error E2451 test.cpp 27: Undefined symbol 'sometext' in function english()
Warning W8080 test.cpp 30: 'someText' is declared but never used in function english()

Last edited on
you might not have used the proper include files,

this is another more standard way,

1
2
3
4
5
6
string x="";

cout << "blah blah blah";                          // just to refer that what and where to change.
while(x == "")			
        getline(cin , x, '\n');


regarding the second question, are u looking for just a method to break down a space seperated words in a string to an array of strings ? or is it something else
Last edited on
thanks thats more helpful.

For the second question, Ive already figured it out. I ended up using x.size() to get the length of whatever was entered and subtracted it by "m". m being whatever character position i needed to change at that time.

appreciate the help.
1
2
3
4
5
6
#include <iostream>
#include <string>
using namespace std;
void explode_string
   ( std::string const & source, string_vector_type & parts
   )

i am testing out a code that i found to separate a string into an array of words

type name expected, comes up for this section of code? what is needed to get rid of this error?
Last edited on
see the thing is the stuff "string_vector_type" is not some thing standard which c++ can understand directly. You have to pass on the header file in which they have declared this "string_vector_type". To me it seems that string_vector_type should be nothing more than:

typedef vector<string> string_vector_type;

but em not at all sure about it. Find out the declaration file, in which this is declared.
k thanks.....Ill do just that

Also, lets say i were to search through a text file for a word, is there a way to get the line and char position of the word, because then I will need to use that position to check another text file and get another word.

Topic archived. No new replies allowed.