Thread: cin strings till eof

  1. #1
    Registered User
    Join Date
    Oct 2003

    cin strings till eof

    this program should read in strings from a file (cannot be opened in the program) until eof, determine if string that was just read in already exists, and if not, prints it out and saves it in the array of strings.

    I can get it to input a single string ie. "test data", and it breaks it up into single letters. It would print, t e s t d a. Ofcourse with end lines where there are spaces. So its testing if the single character exist but I need it to test for the whole string.

    ive messes around with cin.getline and some other things but couldnt get then to work. Is there a better way to input the strings from the file until eof? other than the way Im trying?

    thanks in advance!

    #include <iostream>
    using namespace std;
    #include <iomanip>
    using std::setw;
    const int strng_sz = 20;
    char Array[50][strng_sz];	// Array to hold the numbers w/o duplicates
    char newChar[strng_sz];
    int n = 0;	             	// The number of distinct numbers read so far
    int j;	                  	// Index for inner loop
    while (cin >> newChar[strng_sz])	// Loop to read input
       Array[n][strng_sz] = newChar[strng_sz];		// Store it as a sentinel at the end
                      		                     	// of the array.
       for (j = 0; Array[j][strng_sz] != newChar[strng_sz]; j++);       // Note empty body of loop
       if (j==n)			// Condition for newNum NOT to be a duplicate
         cout << newChar[strng_sz] << endl;
         n++;		       	// Add newNum permanently to the array
    return 0;

  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    University of Waterloo
    your code looks very good, nice job with that, but i see what you mean about the endline thing. I can't really think of an easy way around it, so i'll just give you some tips on how i'd have done can take 'em or leave 'em, they're just here for help

    the way i would do it is to get the string from the user using cin.getline. Once you have that, if it's the first time the user has entered a string, then it must be unique. If it's not the first time, then run through your array of inputted strings to see if the user has put the string in yet. If they haven't, put the new string at the end and increment the counter.

    Also, you can add case-sensitivity in there by converting the string tolower or toupper before placing it in the array, or just doing it on the fly when comparing so as not to lose any data.

    I made a program that shows this, if you would like to see it, please feel free to ask me, but i'd advise that you try to see if you can figure it our yourself given the tips above

    here's some pseudocode, that above paragraph might be confusing.....

    Create the array of strings with any number of lengths and sizes
    initialize the counter of unique strings to 0
    prompt the user for the string
    convert the whole string to lowercase
    ---End Optional---
    is it the first string entered?
         Add it to the first slot in the array and increment the counter
         run through the list of strings
             compare the strings in the array to the one inputted, if none of them are the same, it must be unique-
                  if it was unique, add it to the end of the array and increment the counter
    repeat until the user wants to quit (or whatever you choose to do)
    Last edited by jverkoey; 10-14-2003 at 10:58 PM.

  3. #3
    Registered User
    Join Date
    Oct 2003
    Another very simple way to do this would be to read in the lines and insert each one into a std::set. If the insertion succeeds print out the line. If the insertion fails (sets do not allow duplicates) you know you have a duplicate.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. EOF or not EOF?
    By CornedBee in forum Linux Programming
    Replies: 2
    Last Post: 09-14-2007, 02:25 PM
  2. Problem with Strings, Please help!
    By varus in forum C++ Programming
    Replies: 8
    Last Post: 11-27-2006, 11:47 PM
  3. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  4. finding strings in strings
    By watshamacalit in forum C Programming
    Replies: 14
    Last Post: 01-11-2003, 01:08 AM
  5. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 02:09 AM