• Najnowsze pytania
  • Bez odpowiedzi
  • Zadaj pytanie
  • Kategorie
  • Tagi
  • Zdobyte punkty
  • Ekipa ninja
  • IRC
  • FAQ
  • Regulamin
  • Książki warte uwagi

question-closed Program wyświetlający w pętli tekst piosenki

Object Storage Arubacloud
+1 głos
357 wizyt
pytanie zadane 15 października 2021 w C i C++ przez MKolaj15 Bywalec (2,270 p.)
zamknięte 17 października 2021 przez MKolaj15

Witam, mam do zrobienia zdanie, które polega na wyświetleniu w pętli tekstu piosenki. Zaczynając od 99, lub liczby podanej jako argument w wierszu poleceń, porgram ma wypisać:

99 bottles of beer on the wall, 99 bottles of beer.
Take one down and pass it around, 98 bottles of beer on the wall.

Po osiągnięciu 0 ma wypisać:

No more bottles of beer on the wall, no more bottles of beer.
Go to the store and buy some more, 99 bottles of beer on the wall.

Z wypisaniem tekstu piosenki przez pętle po prostu zaczynając od 99 nie miałem problemu, lecz nie wiem co zrobić, aby kod zadział przy ewentualności podania argumentu przez użytkownika. Oto mój kod:

#include <iostream>
#include <string>


auto main(int argc, char* argv[])->int
{

      auto liczba = std::stoi(argv[1]);
 
      if(argc==0)
      {
        for(auto i=99; i>=0; i--)
        {
          if(i==0)
          {
            std::cout<<"No more bottles of beer on the wall, no more bottles of beer."<<"\n"<<"Go to the store and buy some more, 99 bottles of beer on the wall..."<<"\n";
          }
          else if(i==2)
          {
           std::cout<<i<<" bottles of beer on the wall, "<<i<<" bottles of beer."<<"\n"<<"Take one down, pass it around, "<<i-1<<" bottle of beer on the wall..."<<"\n"; 
          }
          else if(i==1)
          {
           std::cout<<i<<" bottle of beer on the wall, "<<i<<" bottle of beer."<<"\n"<<"Take one down, pass it around, no more bottles of beer on the wall..."<<"\n"; 
          }
          else
          {
            std::cout<<i<<" bottles of beer on the wall, "<<i<<" bottles of beer."<<"\n"<<"Take one down, pass it around, "<<i-1<<" bottles of beer on the wall..."<<"\n";
          }
        }
      }
      else if(argc==1)
      {
        for(auto i=liczba; i>=0; i--)
        {
          if(i==0)
          {
            std::cout<<"No more bottles of beer on the wall, no more bottles of beer."<<"\n"<<"Go to the store and buy some more, 99 bottles of beer on the wall..."<<"\n";
          }
          else if(i==2)
          {
           std::cout<<i<<" bottles of beer on the wall, "<<i<<" bottles of beer."<<"\n"<<"Take one down, pass it around, "<<i-1<<" bottle of beer on the wall..."<<"\n"; 
          }
          else if(i==1)
          {
           std::cout<<i<<" bottle of beer on the wall, "<<i<<" bottle of beer."<<"\n"<<"Take one down, pass it around, no more bottles of beer on the wall..."<<"\n"; 
          }
          else
          {
            std::cout<<i<<" bottles of beer on the wall, "<<i<<" bottles of beer."<<"\n"<<"Take one down, pass it around, "<<i-1<<" bottles of beer on the wall..."<<"\n";
          }
        }
      }
      else
      {
        std::cout<<"Podales niewlasciwe argumenty"<<"\n";
      }

    return 0;
}

Byłbym bardzo wdzięczny, gdyby ktoś mi pomógł, lub nakierował. Z góry dzięki!

komentarz zamknięcia: uzyskałem odpowiedź

1 odpowiedź

+1 głos
odpowiedź 16 października 2021 przez VBService Ekspert (252,660 p.)
wybrane 17 października 2021 przez MKolaj15
 
Najlepsza

[ Edit ]

W tym wypadku Moim zdaniem wystarczy sprawdzić, czy argc jest większy od jeden.

auto how_many_bottles = (argc > 1) ? std::stoi(argv[1]) : 99;

wtedy nie ma potrzeby stosować if-ów w stylu

if(argc==0)  ...
else if(argc==1)  ...

 

przykład [ on-line ]

#include <iostream> 
 
auto main(int argc, char* argv[])->int
{
    auto how_many_bottles = (argc > 1) ? std::stoi(argv[1]) : 99;
  
    for (auto i=how_many_bottles; i>=0; i--)
    {
        if (i == 0)
        {
            std::cout << "No more bottles of beer on the wall,"
                      << " no more bottles of beer.\n"
                      << "Go to the store and buy some more, " << how_many_bottles
                      << " bottles of beer on the wall.\n" << std::endl;
        } 
        else
        {
            std::cout << i << " bottles of beer on the wall, "
                      << i << " bottles of beer.\n"
                      << "Take one down and pass it around, ";
                      (i == 1) ? std::cout << "no more" : std::cout << i-1;
            std::cout << " bottles of beer on the wall.\n" << std::endl;
        }
    }
 
    return 0;
}

lub [ on-line ]

#include <iostream>
 
auto main(int argc, char* argv[])->int
{
    auto how_many_bottles = (argc > 1) ? 
                            ((std::stoi(argv[1]) > 0) ? std::stoi(argv[1]) : 99) : 99;
  
    for (auto i=how_many_bottles; i>0; i--)
    {
        std::cout << i << " bottles of beer on the wall, "
                  << i << " bottles of beer.\n"
                  << "Take one down and pass it around, ";
                  (i == 1) ? std::cout << "no more" : std::cout << i-1;
        std::cout << " bottles of beer on the wall.\n" << std::endl;
    }
    
    std::cout << "No more bottles of beer on the wall,"
              << " no more bottles of beer.\n"
              << "Go to the store and buy some more, " << how_many_bottles
              << " bottles of beer on the wall.\n" << std::endl;
 
    return 0;
}

 

 

endl vs "\n" (New Line) in C++ ]

 

P.S

Conditional ternary operator ?

 

condition ? result1 : result2
If condition is true, the entire expression evaluates to result1, and otherwise to result2.

condition ? ((condition) ? subresult1: subresult2) : result2
                      -------------------- result1 --------------------

komentarz 16 października 2021 przez MKolaj15 Bywalec (2,270 p.)
Naprawdę wielkie dzięki, bardzo mi pomogłeś, lecz wciaż, gdy nie podamy żadnego argumentu, to program wywala błąd :(
komentarz 17 października 2021 przez VBService Ekspert (252,660 p.)
edycja 17 października 2021 przez VBService

No tak mój błąd, uruchom ten prosty kod (bez podawania argumentu) i będzie wszystko jasne.

#include <iostream> 
  
auto main(int argc, char* argv[])->int
{
    std::cout << "argc=" << argc << "\n"
              << "argv[0]=" << argv[0];

    return 0;
}

zapomniałem, że program uruchomiony bez parametru (argumentu-ów) zawiera jeden domyślny argument (ścieżka skąd został uruchomiony program), jeżeli użytkownik uruchomi program i poda argument lub argumenty to argc będzie zawierało wartość większą od 1.

[ on-line ]

#include <iostream> 
  
auto main(int argc, char* argv[])->int
{
    std::cout << "argc " << argc << "\n";
    for (auto i=0; i<argc; ++i) {
        std::cout << "argv[" << i << "] " << argv[i] << "\n";
    }
}

 

1
komentarz 17 października 2021 przez MKolaj15 Bywalec (2,270 p.)
Działa, super, dzięki za pomoc!

Podobne pytania

0 głosów
1 odpowiedź 1,270 wizyt
pytanie zadane 30 maja 2015 w C i C++ przez niezalogowany
0 głosów
0 odpowiedzi 236 wizyt
pytanie zadane 27 stycznia 2019 w C i C++ przez Xeratin Nowicjusz (120 p.)
+1 głos
2 odpowiedzi 274 wizyt
pytanie zadane 10 lutego 2016 w Systemy operacyjne, programy przez natrov Gaduła (3,970 p.)

92,549 zapytań

141,392 odpowiedzi

319,518 komentarzy

61,934 pasjonatów

Motyw:

Akcja Pajacyk

Pajacyk od wielu lat dożywia dzieci. Pomóż klikając w zielony brzuszek na stronie. Dziękujemy! ♡

Oto polecana książka warta uwagi.
Pełną listę książek znajdziesz tutaj.

Akademia Sekuraka

Kolejna edycja największej imprezy hakerskiej w Polsce, czyli Mega Sekurak Hacking Party odbędzie się już 20 maja 2024r. Z tej okazji mamy dla Was kod: pasjamshp - jeżeli wpiszecie go w koszyku, to wówczas otrzymacie 40% zniżki na bilet w wersji standard!

Więcej informacji na temat imprezy znajdziecie tutaj. Dziękujemy ekipie Sekuraka za taką fajną zniżkę dla wszystkich Pasjonatów!

Akademia Sekuraka

Niedawno wystartował dodruk tej świetnej, rozchwytywanej książki (około 940 stron). Mamy dla Was kod: pasja (wpiszcie go w koszyku), dzięki któremu otrzymujemy 10% zniżki - dziękujemy zaprzyjaźnionej ekipie Sekuraka za taki bonus dla Pasjonatów! Książka to pierwszy tom z serii o ITsec, który łagodnie wprowadzi w świat bezpieczeństwa IT każdą osobę - warto, polecamy!

...