Results 1 to 6 of 6

Thread: string

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User marto96's Avatar
    Join Date: May:2008
    Location:
    Posts: 2,211

    string

    . ( ), - . , - .

    :
    Code:
    #include <iostream>
    using namespace std;
    
        struct Tourist
               {
                      char names[50];
                      int age;
               };
               
        int main()
            {
                  Tourist tourists[30];
    
                  int n;
                  cout<<"Enter number of tourists: ";
                  cin>>n;
                  cout<<endl;
                         
                         int i;
                         for(i=0;i<n;i++)
                                        {               
                                         cout<<"Enter three names: ";
                                         cin>>tourists[i].names;
                                         cout<<"Enter age: ";
                                         cin>>tourists[i].age;
                                         cout<<endl;            
                                        }
                         
                                          
                         int max=tourists[0].age;
                         string nmp;
                         for(i=0;i<n;i++)
                                          {
                                           if(tourists[i].age>max)
                                           {
                                            max=tourists[i].age;
                                            nmp=tourists[i].names;
                                           }
                                          }
                                          cout<<"The oldest person is: "<<nmp;
                                          cout<<endl;
                                          
                                          
                                          
        system("pause>nul");
        return 0;
            }
    Dev C++, Code Blocks. , "else nmp=tourists[0].names;", .
    ASRock B450M Pro4 / 5700X@4.5GHz / MSI RX 6600 XT Mech 2X / be quiet! Dark Rock 4 / 2x16GB FlareX 3200MHz CL16 / FSP Hydro G Pro 1000W / Samsung Odyssey G5 S27AG50

  2. #2
    XaMaB's Avatar
    Join Date: Nov:2001
    Location:
    Posts: 20,387
    nmp, -.
    - , string-a
    : XaMaB; . 0.42

    In God we Trust (all others must submit a X.509 certificate). , ()

  3. #3
    Registered User
    Join Date: Sep:2013
    Location: Bulgaria
    Posts: 59
    VS 2013 Express . "Enter three names: " 3 . cin, enter space 3 . . char string getline.
    :

    Code:
    struct Tourist
               {
                      string names;
                      int age;
               };
    // ....
    
    int main()
    {
    // .....
    
    getline(cin, names);
    
    //....
    }
    ..: getline Enter, string- "\n". :

    Code:
    //...
    int n;
    string names;
    //...
    cin>>n; /*   Enter    ,     ,  names   "\n" */
    
    cin.ignore(1, '\n'); //.  names    "\n"
    getline(cin,names);
    
    //...

  4. #4
    Registered User marto96's Avatar
    Join Date: May:2008
    Location:
    Posts: 2,211
    cin.getline, . string .
    ASRock B450M Pro4 / 5700X@4.5GHz / MSI RX 6600 XT Mech 2X / be quiet! Dark Rock 4 / 2x16GB FlareX 3200MHz CL16 / FSP Hydro G Pro 1000W / Samsung Odyssey G5 S27AG50

  5. #5
    Registered User
    Join Date: Dec:2007
    Location: Sofia
    Posts: 366
    - , ( 'if' ).

  6. #6
    Registered User ined's Avatar
    Join Date: Nov:2006
    Location: Varna
    Posts: 1,078
    max , ?

    Code:
    #include <iostream>
    
    using namespace std;
    
    #define MAXT 30
    #define MAXN 50
        
        struct Tourist
               {
                      char names[MAXN];
                      int age;
               };
               
        int main()
            {
                  Tourist tourists[MAXT];
    
                  int i, n, max=-1;
                  
                  do {
                       cout<<"Enter number of tourists[1.."<<MAXT<<"]: ";
                       cin>>n;
                       cout<<endl;
                       } while ((n<1)||(n>MAXT));
                         
                  for(i=0;i<n;i++) {
                                                       
                       cout<<"Enter three names: ";
                       cin.ignore(1, '\n');
                       cin.getline(tourists[i].names,999,'\n');
                       tourists[i].names[MAXN-1] = '\0';
                                                 
                       cout<<"Enter age: ";
                       cin>>tourists[i].age;
                       cout<<endl;  
                       if (max<tourists[i].age) 
                             max = tourists[i].age;          
                  }
                                                               
                  cout<<"The oldest person(s) is: \n";
                  for (i=0;i<n;i++) 
                      if (tourists[i].age==max) 
                           cout<<tourists[i].names<<endl;
                  
                  system("pause>nul");
                  return 0;
            }
    [edit] ,
    .
    Last edited by ined; 7th December 2013 at 23:16.
    ASRock B85 Pro4, Pentium G3240, DD3 8GB/1333MHz, 120GB SSD + 1TB + 640GB HDD
    Gigabyte GV-R657OC-1GI, CM B500, ASUS 24B1S1, LCD SAMSUNG SM931BW, 173

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Copyright © 1999-2011 . .
iskamPC.com | mobility.BG | Bloody's Techblog | | 3D Vision Blog |