Thread: Newbie!!please help

  1. #1
    Unregistered
    Guest

    Unhappy Newbie!!please help

    Hi,
    I have just started learning c++,by my own,
    I am stuck on this:
    How to find overall best comptetitor's name and time.???

    Please guide me.........
    thanks in advance........

    #include<iostream>
    using std::cout;
    using std::cin;
    using std::endl;
    using std::ios;

    #include<string>
    using std::string;


    int main()
    {
    string name;
    double time=0.0;
    double time1=0.0;
    double time2=0.0;
    double totalTime=0.0;
    int counter=0;
    int count=0;
    double besttime=0.0;
    double temp=0.0;



    while(name!="end"||"END")//not sure???//
    {
    cout<<"enter ur name"<<endl;
    cin>>name;

    if(name=="end"||name=="END")//if name entered is "end" stop the prog.//
    {
    break;
    }
    else
    {


    cout<<"enter the cycle time"<<endl;
    cin>>time;
    cout<<"enter the swim time"<<endl;
    cin>>time1;
    cout<<"enter the run time"<<endl;
    cin>>time2;

    if(time==0||time1==0||time2==0) //if any of the stage is not completed then total time should not be calculatd//
    {
    cout<<"total time:not completed"<<endl;
    }
    else
    {
    totalTime=time+time1+time2;
    cout<<"total time is"<<totalTime<<endl;
    if (totaltime>besttime)///Not working......???
    {
    besttime=totalTime;
    }

    if(time!=0&&time1!=0&&time2!=0)//if all stages r completed//
    {
    count++;
    }

    counter++;
    }
    }
    }
    cout<<"total no.of competitors"<<counter<<endl;
    cout<<"no. of competitors who completed all stages"<<count<<endl;
    cout<<"best overall competitor"<<endl;
    cout<<"name"<<name<<endl;
    cout<<"time:"<<besttime<<endl;
    return 0;
    }

  2. #2
    Pygmy Monkey ErionD's Avatar
    Join Date
    Feb 2002
    Posts
    408
    I just swooshed through your code but this looks wrong:

    Code:
    while(name!="end"||"END")
    It should be like this:

    Code:
    #include <cstring> // up there
    
    while(strcmp(name,"end")!=0||strcmp(name,"END")!=0)
    strcmp is a function that takes two strings and compares them and if they are equal it returns 0

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    ErionD--Unregistered has declared name to an instance of the STL string class, not a null terminated char array type string. The STL string class does have overloaded == and != operators so they can be used with objects objects of the STL string class.

    However, you can only do one logical expression at a time so this:

    while(name!="end"||"END")

    should be this:

    while(name != "end" || name != "END")

    as was done almost every place else in the program.
    ================
    This logic :

    if (totaltime>besttime)///Not working......???
    {
    besttime=totalTime;
    }


    should keep track of the best time for you, but in a negative sense, it keeps track of the "slowest" time by a competitor who completed. I presume you want the lowest time to be the best time. If so then assign besttime to be something like 100000000 someplace before the loop and use the < operator rather than the > like this:

    if(totaltime<besttime)
    ===============================
    besttime is a double. Sometime later in your carreer you will learn about structs/classes that will allow you to use a more recognizeable time format.
    ===============================
    When you go to print out the name of the competitor with the best time you will be printing out the name of the competitor that was last entered in the loop because you didn't keep track of the competitors name when you analyzed the totaltime with besttime. Declare another variable called firstplace or something and make it a string. if names time is less than the current besttime then assign name to firstplace.
    ==============================
    the keyword break doesn't end the program, it just breaks you out of the current loop, which is what you want, presumable. there is an exit() function which allows you to safely exit the program at any time without completing the entire program, if that is what you want to do.
    ==============================
    Instead of keeping track of two separate variables having to do with the first place competitor (besttime, firstplace) later in your career using a struct/class to hold both variables for each competitor will allow you to keep track of just one variable that has both of the above embedded within it. Just something to look forward to!

  4. #4
    Unregistered
    Guest

    Thumbs up thanks

    Thanks a lot ......I will definitely work it out now.........

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie(please help)
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 01-22-2002, 07:34 PM