Thread: Whats wrong with this code?

  1. #1
    Registered User
    Join Date
    Oct 2005
    Location
    Brasil
    Posts
    220

    Whats wrong with this code?

    Code:
    string c,e;
         float d,j,k,l,n;
         int m;
         char a[18];
         cout<<"Type the archive that you want to open:\n";
         cin.getline(a,18);
         cin.ignore();
         ifstream a_file ( a );
         getline(a_file,c);
         getline(a_file,d);
         getline(a_file,e);
         getline(a_file,j);
         getline(a_file,k);
         getline(a_file,l);
         getline(a_file,m);
         getline(a_file,n);
         cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ\n";
         cout<<""<< c <<"\n";
         cout<<""<< d <<"\n";
         cout<<""<< e <<"\n";
         cout<<""<< j <<"\n";
         cout<<""<< k <<"\n";
         cout<<""<< l <<"\n";
         cout<<""<< m <<"\n";
         cout<<""<< n <<"\n";
         cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ\n";
    well, the problem is that the archive that i want to open has to many lines, so i tried to do 1 getline for each line, but its not working it dont compile :/ help plz, ty

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    If it didn't compile, what are the compiler errors?

    Right away, I can see that you are using getline to read in floats and an int. The getline function only reads into a string. Use cin >> to read in floats or ints, or use getline and then convert the string to a float/int using a stringstream. If you do use cin >> to read in the numbers, remember to use ignore() to ignore the newline if you use getline to read the next line.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Location
    Brasil
    Posts
    220
    Code:
    string c,e,d,j,k,l,n,m;
         char a[18];
         cout<<"Digite o nome da pessoa que voce quer abrir o registro:\n";
         cin.getline(a,18);
         cin.ignore();
         ifstream a_file ( a );
         getline(a_file,c);
         getline(a_file,d);
         getline(a_file,e);
         getline(a_file,j);
         getline(a_file,k);
         getline(a_file,l);
         getline(a_file,m);
         getline(a_file,n);
         cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ\n";
         cout<<""<< c <<"\n";
         cout<<""<< d <<"\n";
         cout<<""<< e <<"\n";
         cout<<""<< j <<"\n";
         cout<<""<< k <<"\n";
         cout<<""<< l <<"\n";
         cout<<""<< m <<"\n";
         cout<<""<< n <<"\n";
         cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ\n";
    ok. i did this and the program has compiled but it´s not showing in the screen the cout omg :/
    Last edited by Scarvenger; 10-25-2005 at 09:16 AM. Reason: wrong code tag

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    I wonder what you'd do if you had 1,000 lines in that file?

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> it´s not showing in the screen the cout

    You don't need that cin.ignore() there. It means you have to hit enter before the stuff is output. The ignore() is only useful after cin>>.

    Also, add a cin.get() at the end of the code if your console window is closing.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is wrong in this simple code
    By vikingcarioca in forum C Programming
    Replies: 4
    Last Post: 04-23-2009, 07:10 AM
  2. what is wrong with this code please
    By korbitz in forum Windows Programming
    Replies: 3
    Last Post: 03-05-2004, 10:11 AM
  3. I cant find what is wrong with this code
    By senegene in forum C Programming
    Replies: 1
    Last Post: 11-12-2002, 06:32 PM
  4. Anyone see what is wrong with this code?
    By Wise1 in forum C Programming
    Replies: 2
    Last Post: 02-13-2002, 02:01 PM
  5. very simple code, please check to see whats wrong
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-10-2001, 12:51 AM