Problem in the output of the for loop ..

This is a discussion on Problem in the output of the for loop .. within the C++ Programming forums, part of the General Programming Boards category; Hi All ... I'm having a problem in the output of the last for loop ... This is the program ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    9

    Unhappy Problem in the output of the for loop ..

    Hi All ...

    I'm having a problem in the output of the last for loop ...

    This is the program :

    Code:
    #include <iostream>
    #include <iomanip>
    #include <conio>
    
    int main()
    {
       float A[2][3] ;
       int i,j,key,row,col;
    
    
       cout<< "\enter array elements:" << endl;
       for ( i=0 ; i<2 ; i++ )
       {  for ( j=0 ; j<3 ;j++ )
            cin>> A[i][j];
       }
    
    
      cout<< "array elements are: \n" ;
      for ( i=0 ; i<2 ; i++ )
      {  for ( j=0 ; j<3 ;j++ )
       { cout<<  setw(6) << A[i][j];
       }
      cout<< endl;
      }
    
      cout<< "enter the key you want to find :  ";
      cin>> key;
      for ( i=0 ; i<2 ; i++ )
        for ( j=0 ; j<3 ; j++ )
        {  if ( A[i][j] == key )
          {  row=i;
             col=j;
             cout<< key << " was found at row " << (row+1) <<" col "<< (col+1);
             break;
          }
          else
          {
           cout<< key << " was not found" ;
           break;
          }
       }
    
    
    
    getch();
    return 0;
    }
    The output is :

    Code:
    enter array elements:
    1 2 3 4 5 6
    array elements are:
         1     2     3
         4     5     6
    enter the key you want 4
    4 was not found4 was found at row 2 col 1
    The problem is in the last loop .. if the value is found it will print its location and the other output if its not found ..

    This is the output for unfound value :

    Code:
    enter array elements:
    1 2 3 4 5 6
    array elements are:
         1     2     3
         4     5     6
    enter the key you want to find : 9
    9 was not found9 was not found
    How to fix it .. ??

    Any help is Greatly Appreciated ..

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    What exactly do you want?
    Your loop cannot possibly be right. You have
    Code:
    for(...)
     for(...)
     {
       if (key) ... break; 
       else ... break;
     }
    Whatever happens it breaks. No good. You want it to break when the first key was found? In that case:
    Code:
    row = -1;
    for(...)
     for(...)
     {
       if (key) ... break;
     }
    if (row == -1)
       cout << "Key not found" << endl;
    else
       cout << "Key found on row " << row << " column " << col << endl;

  3. #3
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by Kawaii JoJo View Post
    Hi All ...

    I'm having a problem in the output of the last for loop ...

    This is the program :

    Code:
    #include <iostream>
    #include <iomanip>
    #include <conio>
    
    int main()
    {
       float A[2][3] ;
       int i,j,key,row,col,flag=0;
    
    
       cout<< "\enter array elements:" << endl;
       for ( i=0 ; i<2 ; i++ )
       {  for ( j=0 ; j<3 ;j++ )
            cin>> A[i][j];
       }
    
    
      cout<< "array elements are: \n" ;
      for ( i=0 ; i<2 ; i++ )
      {  for ( j=0 ; j<3 ;j++ )
       { cout<<  setw(6) << A[i][j];
       }
      cout<< endl;
      }
    
      cout<< "enter the key you want to find :  ";
      cin>> key;
      for ( i=0 ; i<2 ; i++ )
        for ( j=0 ; j<3 ; j++ )
        {  if ( A[i][j] == key )
          {  row=i;
             col=j;
             cout<< key << " was found at row " << (row+1) <<" col "<< (col+1);
             flag = 1;
             break;
          }
          else // remove this else
          {
           cout<< key << " was not found" ;
           break;
          }   }
    
    // if(!flag) printf("key not found");
    
    getch();
    return 0;
    }
    The output is :

    Code:
    enter array elements:
    1 2 3 4 5 6
    array elements are:
         1     2     3
         4     5     6
    enter the key you want 4
    4 was not found4 was found at row 2 col 1
    The problem is in the last loop .. if the value is found it will print its location and the other output if its not found ..

    This is the output for unfound value :

    Code:
    enter array elements:
    1 2 3 4 5 6
    array elements are:
         1     2     3
         4     5     6
    enter the key you want to find : 9
    9 was not found9 was not found
    How to fix it .. ??

    Any help is Greatly Appreciated ..
    You input a key and only when you've traversed the whole array you'll get to know if the key is present or not. How can you say that the key is not present before traversing the whole array? So, remove the else statement and put it after the array has been traversed with the condition that the key is not found.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Infinite loop output problem
    By Jonyb222 in forum C Programming
    Replies: 5
    Last Post: 10-08-2009, 12:18 PM
  2. Problem with my output
    By Dogmasur in forum C Programming
    Replies: 17
    Last Post: 08-07-2008, 09:07 PM
  3. output from bank type problem
    By IzaakF in forum C Programming
    Replies: 2
    Last Post: 09-04-2002, 07:42 PM
  4. File input output problem
    By VanJay011379 in forum C++ Programming
    Replies: 2
    Last Post: 08-30-2002, 03:09 PM
  5. String Output Problem
    By Yin in forum C++ Programming
    Replies: 3
    Last Post: 03-14-2002, 07:36 AM

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