Hello everyone :)

My C++ class just started learning strings the other day. We were given a simple program that's due tomorrow, but I'm having a bit of trouble with it. I've emailed my teacher, who says I'm on the right track but need to fix something in my while loop.

The program asks the user to enter all or part of a city name. Then the program locates the file cities.txt and ouputs any matches. At the end, the matches are added up for a total.

This is what I have:

#include <iostream>
#include <string>
#include <fstream>
#include <conio.h>
using namespace std;

int main()
{
	ifstream file;
    string s, city, bigstring, substring;
    int count = 0; // Count variable to count the number of matches within the file.
    file.open("c:\\cities.txt"); // Opens the file cities.txt.

	cout << "Enter all or part of a city name: ";
	getline(cin,city); // User-inputted city name.

	if (bigstring.find(substring) != -1) // Returns the position of substring within bigstring. If substring doesn't exist in bigstring, -1 is returned.

	while (getline(file,s)) // Read a line. If successful, enter the loop.
    {
        cout << s << endl;
        count++
    }

    cout << "There were " << count
        << " matches in the file" << endl;

	getch();
	return 0;
}

He said that my problem is this:

while (getline(file,s)) // Read a line. If successful, enter the loop.
    {
        cout << s << endl;
        count++;
    }

That, "That's printing every line you read. Wouldn't you want to conditionally print the city instead?"

But, I feel like I've tried everything...any suggestions?

You should be putting your if statement inside the while loop. The way the program should work is you should read in a city name and then check to see if that city matches or partially matches the city the user entered. If it does output the city name that you read in. If it does not the return to the top of the loop.

I think you have lots of problems there.

First what do you do with "city" after you get it with :getline(cin,city);

Second you are searching substring in bigstring, but you did not initialize those strings? What you are doing is searching zero in zero ?

Third you are reading every line in the file cities.txt. What is the point of doing that? You may want to search the file for the substring instead i think, and display the matching cities.

Tellalca,

My teacher specifically wrote the line: if bigstream.find(substring) != -1, so I dont think we're supposed to change that. I did put it in the loop, but I'm still getting the same output (the entire file is read). After the program recieves getline(cin,city), it's supposed to read through the file for matches, but I'm not sure how to go about doing that :(

Well if you have to have bigstring ang substring then you can do

substring = city;
while (getline(file, s))
{
    bigstring = s;
    //...
}

That didn't work either. I feel like I'm at a loss, here, as I feel like I've tried everything. I've tried changing s to city, bigstring or substring, moving things around...I don't know what else to try

#include <iostream>
#include <string>
#include <fstream>
#include <conio.h>
using namespace std;

int main()
{
	ifstream file;
    string s, city, bigstring, substring;
    int count = 0; // Count variable to count the number of matches within the file.
    file.open("c:\\cities.txt"); // Opens the file cities.txt.

	cout << "Enter all or part of a city name: ";
	getline(cin,city); // User-inputted city name.
	substring = city;

	while (getline(file,s)) // Read a line. If successful, enter the loop.
    {
		if (bigstring.find(substring) != -1) // Returns the position of substring within bigstring. If substring doesn't exist in bigstring, -1 is returned.
		bigstring = s;
		cout << s << endl;
        count++;
    }

    cout << "There were " << count
        << " matches in the file" << endl;

	getch();
	return 0;
}

Move line 21 above line 20 and make cout << s; part of the if statement.

bigstring = s;
if (bigstring.find(substring) != -1)
{
    cout << s << endl;
}

Edited 6 Years Ago by NathanOliver: n/a

It works! Thank you so much! :) I know it probably seemed like a simple mistake, but I had been trying to figure it out all day :\

This article has been dead for over six months. Start a new discussion instead.