Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8193463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:29:18+00:00 2026-06-07T04:29:18+00:00

I’m writing a program that will guess words taken from a big text file.

  • 0

I’m writing a program that will guess words taken from a big text file. One step is taking user input to determine the length of the string.

edit: added full code, made some changes

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int i(0),n(0),counter(0),limit(0);
char words[60000][30];

int initarray() {

 int length(0);
 string line;
 char temp;

 ifstream wordlist ("text.txt");

 if (wordlist.is_open())
 {
     while (wordlist.good())
     {
         getline (wordlist,line);
         length=line.length();

         for (n=0;n!=length;n++)
         {
             temp=line.at(n);
             words[i][n]=temp;
         }
         i++;
         counter++;
     }
 }
 else
 {
    cout<<"file not opened";
 }
 wordlist.close();
 return 0;
}

int selectlength()
{
 int length;
 bool shorter(false),longer(false);

 cout <<"length of word"<<endl;
 cin >> length

 limit=counter;
 counter=0;

 for (i=0;i<limit;i++){

    for (n=0;n!=length;n++){
        if (words[i][n]=='\0')
        {
            shorter=true;
            break;
        }
    }

    if (words[i][length+1] != '\0')
    {
        longer=true;
    }

    if (longer==true || shorter==true)
    {
        i--;
     }
 }
    return 0;
}


int printresults(){
 for (i=0;i!=counter;i++){
     for (n=0;n<=20;n++){
         cout << words[i][n];
     }
     cout <<endl;
 }
 return 0;
}

int main() {
 initarray();
 selectlength();
     printresults();
 return 0;

}

but my problem is happens whenever the program, which compiles fine, gets to the “cin” part to read user input for the length. When I enter in any number and hit enter, nothing happens. The program is still running, and just keeps taking input indefinately. Any help? Could it have anything to do with my using ifstream earlier in the prigram, albeit in a different function?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T04:29:19+00:00Added an answer on June 7, 2026 at 4:29 am

    You’ve got an infinite loop in selectlength(). The outer for loop doesn’t terminate because you’re decrementing i (the loop counter) inside the loop (not a good idea, perhaps find a better way).

    I think you’re not terminating on the last line in the input file. longer and shorter will both be true and limit will never be reached. Put in a test for this in the loop:

    if (words[i][0] == '\0')
        break;
    

    That will at least stop the infinite loop and allow you to relook at your logic (it’s not clear what longer and shorter will be used for.

    A few general comments:

    1. Putting trace statements inside the problem area can help you identify a problem.
    2. You’ll learn more about C++ if instead of the char words[x][y] you use std::vector<std::string> words;
    3. A boolean inside an if statement is easier to read like this: if (longer || shorter) than how you have it.
    4. You’re always returning 0 – rather make the function void.

    You’re also setting the global counter to 0 inside selectlength() but you still need it later in printresults() so you’ll get no output.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I need a function that will clean a strings' special characters. I do NOT
I have a reasonable size flat file database of text documents mostly saved in
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.