Thread: Newbie Question on a Loop/If-Else thing

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    10

    Newbie Question on a Loop/If-Else thing

    Here is some code that is probably all f'd up:

    int main()
    {
    int year;
    char ans;
    ans = y;
    while(ans==y)
    {
    cout<<"What Year?";
    cin>>year;
    cout<<endl<<year<<endl;
    date(year);
    cout<<"Would you like another date? (y/n)";
    cin>>ans;
    }
    cout<<"Thank you"<<endl;
    return 0;
    }

    It keeps telling me " y undeclared first use the function, i have tried a million variation, i dont know what the deal is. Here is the same code in If-Else format, i think.

    int main()
    {
    int year;
    bool ans;
    cout<<"Would you like to enter a date?(y/n):";
    cin>>ans;
    if(ans==y)
    {
    cout<<"Enter the year(for example, 1999):";
    cin>>year;
    cout<<endl<<year<<endl;
    date(year);
    cout<<"Would you like to enter another date?(y/n):";
    cin>>ans;
    }
    else
    {
    cout<<"Thank you"<<endl;
    }
    return 0;
    }

    Any help would be great. Oh yeah the if then has the same y is undeclared thing.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    11

    try this

    int main()
    {
    int year;
    char ans;
    ans = y;
    while(ans==y)

    with this code you have created an endless loop. ans will always equal y in this case. By the way, your syntax should be:

    ans = 'y';
    if(ans=='y')

    you need to get the input from the user first and then do the while loop to check and see if it is y.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    10
    Thanks, i figured it out, well, you figured it out anyways. I switched back to while and put the ' ' around y it it works fine now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stupid Newbie question
    By TimL in forum C++ Programming
    Replies: 4
    Last Post: 07-22-2008, 04:43 AM
  2. Newbie with Very Newbie Question
    By Jedi_Mediator in forum C++ Programming
    Replies: 18
    Last Post: 07-01-2008, 08:00 AM
  3. newbie: array question :(
    By cstudent in forum C Programming
    Replies: 2
    Last Post: 04-09-2008, 06:46 AM
  4. Total Newbie Question
    By Kid A in forum C++ Programming
    Replies: 4
    Last Post: 06-22-2006, 05:36 AM
  5. Newbie Question - fflush(stdin) & fpurge(stdin) on Mac and PC
    By tvsinesperanto in forum C Programming
    Replies: 34
    Last Post: 03-11-2006, 12:13 PM