istringstream problem?

Write your question here.

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
// This is my code in main: 

string word, processed_word, scrambled_word; 
string first_letter, last_letter, final_word; 
string line; 

ifstream in_file("input.txt"); 
ofstream out_file("output.txt"); 
getline(in_file, line); 
istringstream iss(line); 

if (iss >> word) 
{ 

first_letter = word[0]; 
last_letter = word[word.length() -1]; 

processed_word = process_word(word); // function for word to go to 

scrambled_word = shuffle_word(processed_word); // function for word to go to 

final_word = first_letter + scrambled_word + last_letter; // Puts parts of a word back together 

cout << final_word; 
out_file << word; 
} 


I have a program that is supposed to read lines from a file and send each word through a series of functions I made. The functions scramble the "inner" letters of a word.

I have a text file to import that says "Random Words" but when I run the code it only prints Random an infinite number of times, Random once, nothing at all, or Words once.

I'm not sure what I'm doing wrong, aren't streams supposed to read strings one word at a time? Shouldn't it be taking "Random" pass it through the functions, then take "Words" and do it again until there are no more words?
This code is printing the word "Random" with scrambled letters like it's supposed to, but it doesn't keep going onto "Words". If I change it from "if" to "while" is prints Random forever.

What am I doing wrong? How do you get it to go through each word once and then stop?

I've tried all kinds of combinations with for/while loops instead of an if statement. It either only runs the first word infinite times or does nothing and prints a blank space.

I just tried using a for loop with a counter a few minutes ago hoping it would go through the loop multiple times pulling the next word in the string each time...and it was printing nothing too. :\

iss >> word; is the code that should be pulling the next word from the input file's string into the string "word", correct?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <sstream>
#include <iostream>
#include <string>

int main()
{
    std::string line;
    while (std::getline(std::cin, line))
    {
        std::istringstream is(line);
        std::string word;

        while (is >> word)
            std::cout << word << '\n';
    }
}


http://ideone.com/RdNkal
I tried cire's solution using my file name and it's still not printing out anything. :\

It just says "Run Finished".
Topic archived. No new replies allowed.