Thread: Help with a goto loop

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    367

    Unhappy Help with a goto loop

    I want a program that searches for specific infromation on a col through a whole file, by each col. I thought like this but I'm pretty sure of that there's some better code for it:

    int main()
    {

    char text[20];
    cin.getline(text, 20, '\n');

    std::ofstream tfile("test.dat") /* I don't know how to make it search through the actual file */

    while (strcmp(text, "here is the col I search for"))
    {
    std::cout << "*going to the next col*";
    }

    When it hits the actual col, the while loop will stop. Then I also would like to put the actual col in a variable.

    Please help me so that it can search through the file and also
    the "going to next col" code, and finally putting the information on the matching col in a variable. Rewrite the whole code if it's
    so inefficient that you find it neccesary.
    Last edited by Zewu; 02-22-2002 at 02:57 PM.

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    >Rewrite the whole code if it's so inefficient that you find it neccesary

    What code?

    What's the format of test.dat? Does each column contain separated strings (do they contain whitespace)? What's the column delimeter? How many columns are there?

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    367

    This code:

    int main()
    {

    char text[20];
    cin.getline(text, 20, '\n');

    std::ofstream tfile("test.dat") /* I don't know how to make it search through the actual file */

    while (strcmp(text, "here is the col I search for"))
    {
    std::cout << "*going to the next col*";
    }

  4. #4
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    Sorry, it must of been in my blind spot, I'd go with that if I were you; it looks fine.

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    367

    Yes, the syntax looks fine but can anyone help me with the code

    That's all.

  6. #6
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    As there's no magic functions in C++ to detect the format of a file, you're going to have to provide more info (see questions in my original post).

  7. #7
    Registered User
    Join Date
    Dec 2001
    Posts
    367

    Here's what I mean.....

    Ok, I just want to create a program that searches through a file for a col that holds specific information, and then store that specific information in a variable.

    assume that we have a file looking like this:

    trings
    intrgs
    stnrgi
    rtgins
    string
    trgnis
    nstgir

    assume that I want to search for the col which holds the information "string". When the program has found the information "string", I also want that information to be stored in a variable which we can call x.

  8. #8
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    In your code you'll have to place another cin.getline() in the while loop, to read the next line. Everytime it doesn't find the string it'll then read another line.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My loop within loop won't work
    By Ayreon in forum C Programming
    Replies: 3
    Last Post: 03-18-2009, 10:44 AM
  2. Visual Studio Express / Windows SDK?
    By cyberfish in forum C++ Programming
    Replies: 23
    Last Post: 01-22-2009, 02:13 AM
  3. Replies: 8
    Last Post: 12-01-2008, 10:09 AM
  4. syntax question
    By cyph1e in forum C Programming
    Replies: 19
    Last Post: 03-31-2006, 12:59 AM
  5. How to change recursive loop to non recursive loop
    By ooosawaddee3 in forum C Programming
    Replies: 1
    Last Post: 06-24-2002, 08:15 AM