I'm working on an assignment in which we aren't allowed to use arrays or functions. So i need to use loops to split words from a text file. I got most of the code working but i get an error at this part where the loop doesn't exit when i come to a space in the text

code:

         for(int i=0;i<line.length(); i++)
         {
             n++;
             //stringstream(input.substr(n,1)) >> num1;
             stringstream(line.substr(n,1) ) >> character;

             if(character == ' ')
             {
                 break;
             }
             else
             {
             word += character;
             }

         }

why are you using stringstre3am? It's like hitting a nail with a slughammer.

What is variable n for? Just use the loop counter i.

if( line[i] == ' ')
{
   break;
}
else
{

}

that will give you only the first word in the line. What are you going to do with the rest of the words that nave not been processed?

Edited 3 Years Ago by Ancient Dragon

Im not really allowed to use arrays, the professor wants us to all be on the same "level". The n variable is there because after the loop exits, it enters another loop with the word that was extracted from one line of the file. This loop is inside another loop. Ill post the entire code :

// SpellChecker.cpp : Defines the entry point for the console application.
//Name:Mohammad Khan 100506024
//Description: This program checks the spelling of a .txt document that the user provides
//Date:20/03/2012

#include "stdafx.h"
#include<iostream>
#include<cmath>
#include<fstream>
#include<string>
#include<istream>
#include<sstream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
 //variable declartion
 string infile,word,line,check;
 ifstream text,dictionary;
 char character='A';
 bool flag(true);
 int n=0;

 //entering file name
 cout<<"Please enter the name of the file that needs to be checked: ";
 cin>>infile;

 //open files : dictionary and user input
 text.open(infile);
 dictionary.open("dictionary.txt");

 //check if file to check for error exsists
 if(text.fail())
 {
     //output error message
     cout<<"Error File could not be found";
     system("Pause");
 }
 else
 {

//Beginning of main loop
 while(!text.eof())
 {

     //receiving the line from the user provided text file
         n=0;
         getline(text,line);

         //loop for splitting off words

         for(int i=0;i<line.length(); i++)
         {

             //stringstream(input.substr(n,1)) >> num1;
             stringstream(line.substr(n,1)) >> character;

             if(character == ' '|| character=='\n')
             {
                 break; //some wierd error here must fix it latter
             }
             else
             {
             word += character;
             }

             n++;

         }


         //inner loop for comparison of words to words in dictionary
         while(!dictionary.eof())
         {
            //gets the word from the dictionary
            getline(dictionary,check);

            if(check==word)
            {
                break;
                flag=false;
            }
         }
         if(flag==false)
         {
             flag=true;
         }
         else
         {
             cout<<word<<endl;
             word="";
         }

 }//end of main loop

 system("Pause");

 }//end error check statement
 return 0;
}

i ran through the code, and for some reason, the character which is supposed to be a space ends up being the previous character in the text file, any ideas as to why this is happening

This question has already been answered. Start a new discussion instead.