help with these code

This is a discussion on help with these code within the C++ Programming forums, part of the General Programming Boards category; after the program runs the first time, it always skip the gets(name[i]), anyone can tell me where i went wrong? ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    1

    help with these code

    after the program runs the first time, it always skip the gets(name[i]), anyone can tell me where i went wrong?



    Code:
    #include<iostream>
    #include <fstream>
    using namespace std;
    
    char name[41][40], dob[41][20], sch[41][100],tel[41][15], add[41][100];
    char FileName[20], reply,b;
    int i,j,Y,z;
     
    int y;
    int show( int y);
    
    
    main ()
    {
         
    for (i=1; i<=41 ; i++ )
    {
    for (z=i; z <=1; i++){
    cout << "\nEnter the name of the file to create (e.g file.txt or file.doc , etc) : \n";
    gets( FileName);
    z=2;
    }
    
      system("cls");
    
      cout<<"Name : ";
      gets(name[i]);
      cout<<"\nTelephone number : " ;
      gets(tel[i]);
      if(!*name[i]&& !*tel[i]) break;
      cout<<"\nDate of Birth : " ;
      gets(dob[i]);
      cout<<"\nAddress : " ;
      gets(add[i]);
      cout<<"\nSchools : " ;
      gets(sch[i]);
      
    
          cout << "\n\nName      \t: \t" << name[i];
          cout<<"\ntelephone \t: \t" << tel[i];
          cout<<"\nDate of Birth \t: \t" << dob[i];
          cout<<"\nAddress \t: \t" << add[i];
          cout<<"\nSchools      \t: \t" << sch[i];
          cout<< "\n\nSafe to \""<<FileName<<"\"? <Y/N>";
          cin>> reply;
    
    if(reply =='Y' || reply =='y')
    {
    ofstream Students(FileName, ios::app); 
    Students << "\n\n\nName      \t: \t" << name[i];
    Students<< "\ntelephone \t: \t" << tel[i];
    Students<<"\nDate of Birth \t: \t" << dob[i];
    Students<<"\nAddress \t: \t" << add[i];
    Students<< "\nSchools      \t: \t" << sch[i]; 
    }
    
    }
    
    }

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    do you really need all of those variables to be global?
    also is 'int show( int y);' ever defined?

    'main' should be 'int main'
    Code:
    for (i=1; i<=41 ; i++ )
    {
    for (z=i; z <=1; i++){
    cout << "\nEnter the name of the file to create (e.g file.txt or file.doc , etc) : \n";
    gets( FileName);
    z=2;
    }
    this second for loop will always only happen once right?

    anyways to your specific question, use cin or getline(cin,stringName)

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    this second for loop will always only happen once right?
    Wrong
    it will run once for i == 1
    and never runs when i >1

    Also arrays are zero based, so name[0] is never used, but name[41] is used but actually not allocated

    always skip the gets(name[i])
    Try to read the question before entering the data.
    I bet the gets( FileName); is skiped

    And also due to (z=i; z <=1; i++) the element 1 is also skipped
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    Well for a starters you should never use gets() at all, it's totally unsafe.

    > cin>> reply;
    The second problem is mixing input styles.

    I suggest you use cin.getline() to read a whole line into a buffer of some sort, then extract the information you want from that buffer.

    There's plenty of examples of using getline on the board.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Wrong
    it will run once for i == 1
    and never runs when i >1
    thats what i said, it will always only run once.
    Code:
    for (i=1; i<=41 ; i++ )
    {
    for (z=i; z <=1; i++){
    i is set to 1, z is set to i. is z (i) (1) <= 1? yes. it will always run once, therefore no need for this for loop

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    thats what i said, it will always only run once.
    Ok i just read it wrong as "internal loop runs once for each iteration of the external loop". Since you didn't mean it - I'm sorry
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    ok no worries

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 03:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 05:05 PM

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