Whats wrong with this code?

This is a discussion on Whats wrong with this code? within the C++ Programming forums, part of the General Programming Boards category; 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 ...

  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,317
    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 its 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,662
    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,317
    >> its 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, 09: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, 05:32 PM
  4. Anyone see what is wrong with this code?
    By Wise1 in forum C Programming
    Replies: 2
    Last Post: 02-13-2002, 01: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21