Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Please help me with the code ( basic programming )

0 votes
asked Sep 3, 2018 by anonymous
int main()
{
    int x;
    int y;
    cout<<"Unesite broj koja oznacava sate: "<<endl;
    do {
        cin>>x;
        if (x<0 || x>23)
        cout<<"Unijeli ste krivi broj! Pokusajte ponovo..."<<endl;
    }
    while (x<0 || x>23);
    
    cout<<"Unesite broj koji oznacava minute: "<<endl;
    do {
        cin>>y;
        if (y<0 || y>59)
        cout<<"Unijeli ste krivi broj! Pokusajte ponovo..."<<endl;
    }
    while (y<0 || y>59);
    
    cout<<"Trenutno je :"<<x<<":"<<y<<endl;
    
   
    return 0;
}

So it is written that I did not declared cout and endl.... Can you help e please, I would appreicate that a lot, thanks !

2 Answers

0 votes
answered Sep 4, 2018 by Fady Serhan

do you mean that the program is not recgonzing the words "cout" and "endl" ??

if that is the problem then you should add "using namespace std;" above the main() function.

0 votes
answered Sep 5, 2018 by Leroy (390 points)
edited Sep 5, 2018 by Leroy
#include <iostream>
#include <string>

using namespace std;

/*
 add std:: infront of cout, and between endl; example std::endl; disable namespce above with forward // to comment it out, for your output to work, otherwise just add using namespace std; to run your code.
*/

int main()
{
    int x;
    int y;
    cout<<"Unesite broj koja oznacava sate: "<<endl;
    //or they are both the same, just differnt flavour
    printf("Unesite broj koja oznacava sate: /n");
    do {
        cin>>x;
        if (x<0 || x>23)
        cout<<"Unijeli ste krivi broj! Pokusajte ponovo..."<<endl;
    }
    while (x<0 || x>23);
    
    cout<<"Unesite broj koji oznacava minute: "<<endl;
    do {
        cin>>y;
        if (y<0 || y>59)
        cout<<"Unijeli ste krivi broj! Pokusajte ponovo..."<<endl;
    }
    while (y<0 || y>59);
    
    cout<<"Trenutno je :"<<x<<":"<<y<<endl;
    

/*

While is not recommended in your code, while is infinity, and your code is running and is dangerous to use, it can really ruin your code, you should use a if else, then while to catch the error. Exception catching can be handy when a while is thrown it can be caught running in the back ground as a fail safe, or as a listener where the do was inputted, yet the if was fired under a certain condition resulting in a while condition to pick it as either true or false, where you have two while loops running. Stick with if() if else conditions first then switchs, you'll learn that while loops are like on and off switches later.

*/
   
    return 0;
}
commented Sep 16, 2018 by bc170401728 ARIFA BIBI (100 points)
please  teach programing i dont understand anything about it.
commented Sep 19, 2018 by Leroy (390 points)
just copy all i wrote into a ide, or online above and everything will make sense, just play around with it.
i just gave you c prinf("d%", "Unesite broj koja oznacava sate:") flavour.

or  c++ cout<<"Unesite broj koja oznacava sate:"<<endl;  use if using namespace std

 or std::cout<<"Unesite broj koja oznacava sate:"<<std::endl; only use if no using namespace std;
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and receive answers from other members of the community.
...