comparare siruri

Intrebari despre limbajul C++, standardul C++, STL, OOP in C++ sau alte subiecte nelegate de VisualC++
Post Reply
stelian_2681
Junior
Junior
Posts: 36
Joined: 24 Jun 2013, 23:25
Judet: Olt

comparare siruri

Post by stelian_2681 » 24 Jun 2013, 23:54

Se citesc de la tastatura 2 siruri de caractere.
Sa se compare cele doua siruri.

Code: Select all

#include <iostream>
#include <string.h>
#define S 100
using namespace std;

int main()
{
    int x;
    char A[S], B[S];
    cout<<"\n Introduceti primul sir "<<endl<<endl<<" ";
    cin.get(A,100);
    cout<<"\n Introduceti al doilea sir "<<endl<<endl<<" ";
    cin.get(B,100);
    x=stricmp(A,B);
    if(x>0)
        cout<<"\n Primul sir este mai mare "<<endl;
    else if(x==0)
        cout<<"\n Sirurile sunt egale "<<endl;
    else
        cout<<"\n Al doilea sir este mai mare decat primul "<<endl;
    return 0;
}
la rularea acestui cod nu pot introduce al doilea sir
Astept sugestii.
Va multumesc !
Last edited by Marius Bancila on 25 Jun 2013, 12:38, edited 1 time in total.
Reason: code tags



Viorel
Microsoft MVP
Microsoft MVP
Posts: 292
Joined: 13 Jul 2007, 12:26

Re: comparare siruri

Post by Viorel » 25 Jun 2013, 07:52

Încearcă cin.getline.

User avatar
Marius Bancila
Fondator
Fondator
Posts: 2344
Joined: 11 Jul 2007, 11:45
Judet: Timiş
Location: Timisoara
Contact:

Re: comparare siruri

Post by Marius Bancila » 25 Jun 2013, 12:46

Foloseste std::string in loc de char[], include <string> si nu <string.h> si foloseste cum zicea Viorel geline() (cand textul tau contine spatii).

Code: Select all

#include <iostream>
#include <string>

using namespace std;

int main()
{
   string A, B;
   cout<<"\n Introduceti primul sir "<<endl<<endl<<" ";
   std::getline(cin, A);
   cout<<"\n Introduceti al doilea sir "<<endl<<endl<<" ";
   std::getline(cin, B);
   int x = _stricmp(A.c_str(),B.c_str());
   if(x>0)
      cout<<"\n Primul sir este mai mare "<<endl;
   else if(x==0)
      cout<<"\n Sirurile sunt egale "<<endl;
   else
      cout<<"\n Al doilea sir este mai mare decat primul "<<endl;

   return 0;
}
Marius Bancila
Fondator Codexpert, Microsoft MVP VC++
Site personal | Blog

Post Reply