Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,394 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,138 people online right now. Registration is fast and FREE... Join Now!




User Input and Output

2 Pages V  1 2 >  
Reply to this topicStart new topic

User Input and Output

csstarter9
18 Sep, 2006 - 05:15 AM
Post #1

New D.I.C Head
*

Joined: 18 Sep, 2006
Posts: 21


My Contributions
I am trying to write a program that has loops and answer these questions:
1.Request two integer numbers from the user.
2.Verify if the two numbers are between 0 and 10 (exclusive) and, if not, ask for the two numbers again until the conditions are met.
3.Instruct the user to enter the product of the two numbers.
4.If the product entered by the user is incorrect request the product from the user again with a message that the answer was incorrect. Keep requesting the number until the answer is correct.
5.When the product entered by the user is correct output a message specifying so and ask the user if he/she would like to start over.
6.If the answer to the question to start over is 1, start over, and, otherwise, end the program.

here is maybe a bit of the sample code for the loop I have to create for the program:

CPP / C++ / C Code:
CODE

#include <iostream>

using namespace std;

int main()
{
    cout << "This is a while loop example \n";

    bool done;
    int userChoice;

    done = false;

    while (done == false)
    {
        cout << "Enter <1> to continue: ";
        cin  >> userChoice;

        if (userChoice == 1)
        {
            done = true;
        }
    }

    cout << "\n";
    cout << "This is a do-while loop example \n";

    bool finished;

    do
    {
        cout << "Enter <1> to continue: ";
        cin  >> userChoice;

        if (userChoice == 1)
        {
            finished = true;
        }
        else
        {
            finished = false;
        }
    } while (finished == false);

    cout << "\n";
    cout << "This is a for loop example \n";

    int i,nCount,nStep;
    
    cout << "Enter the number to count: ";
    cin  >> nCount;

    cout << "Enter the step Size: ";
    cin  >> nStep;
    
    for (i = 0; i <= nCount; i = i + nStep)
    {
        if (i < nCount)
        {
            cout << i << " and counting... \n";
        }
        else
        {
            cout << i << " and done. \n";
        }
    }

    return 0;
}
{
            cout << i << " and done. \n";
        }
    }

    return 0;
}

User is offlineProfile CardPM
+Quote Post

Amadeus
RE: User Input And Output
18 Sep, 2006 - 05:17 AM
Post #2

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,230



Thanked: 40 times
Dream Kudos: 25
My Contributions
Can you specify the problem you are encountering, with any error messages that may be generated?
User is offlineProfile CardPM
+Quote Post

Xing
RE: User Input And Output
18 Sep, 2006 - 05:22 AM
Post #3

D.I.C Addict
Group Icon

Joined: 22 Jul, 2006
Posts: 723



Thanked: 2 times
Dream Kudos: 1575
My Contributions
You are having extra
CODE
{
cout << i << " and done. \n";
}
}

return 0;
}
in the end. Remove it and your code will compile. Next time use code tags to display your code.

This post has been edited by Xing: 18 Sep, 2006 - 05:22 AM
User is offlineProfile CardPM
+Quote Post

csstarter9
RE: User Input And Output
18 Sep, 2006 - 12:23 PM
Post #4

New D.I.C Head
*

Joined: 18 Sep, 2006
Posts: 21


My Contributions
QUOTE(Amadeus @ 18 Sep, 2006 - 06:17 AM) *

Can you specify the problem you are encountering, with any error messages that may be generated?



I don't know how to make the program do the steps 3-6
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: User Input And Output
18 Sep, 2006 - 03:20 PM
Post #5

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,230



Thanked: 40 times
Dream Kudos: 25
My Contributions
Well, #3 is the same as #1, except you are only asking for one number. Then you multiply the two numbers from #1 together, and check to see if it's the same as the number from #3. From your post, you've already completed 1 and 2, right? #3 is the same type of thing.
User is offlineProfile CardPM
+Quote Post

csstarter9
RE: User Input And Output
19 Sep, 2006 - 12:47 PM
Post #6

New D.I.C Head
*

Joined: 18 Sep, 2006
Posts: 21


My Contributions
QUOTE(Amadeus @ 18 Sep, 2006 - 04:20 PM) *

Well, #3 is the same as #1, except you are only asking for one number. Then you multiply the two numbers from #1 together, and check to see if it's the same as the number from #3. From your post, you've already completed 1 and 2, right? #3 is the same type of thing.


When the product entered by the user is correct output a message specifying so and ask the user if he/she would like to start over.
6.If the answer to the question to start over is 1, start over, and, otherwise, end the program.

here is maybe a bit of the sample code for the loop I have to create for the program:

CPP / C++ / C Code:
CODE

#include <iostream>

using namespace std;

int main()
{
    cout << "This is a while loop example \n";

    bool done;
    int userChoice;

    done = false;

    while (done == false)
    {
        cout << "Enter <1> to continue: ";
        cin  >> userChoice;

        if (userChoice == 1)
        {
            done = true;
        }
    }

    cout << "\n";
    cout << "This is a do-while loop example \n";

    bool finished;

    do
    {
        cout << "Enter <1> to continue: ";
        cin  >> userChoice;

        if (userChoice == 1)
        {
            finished = true;
        }
        else
        {
            finished = false;
        }
    } while (finished == false);

    cout << "\n";
    cout << "This is a for loop example \n";

    int i,nCount,nStep;
    
    cout << "Enter the number to count: ";
    cin  >> nCount;

    cout << "Enter the step Size: ";
    cin  >> nStep;
    
    for (i = 0; i <= nCount; i = i + nStep)
    {
        if (i < nCount)
        {
            cout << i << " and counting... \n";
        }
        else
        {
            cout << i << " and done. \n";
        }
    }

    return 0;
}

ok i removed the extra set of code but how do i do the code for 3-6?
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: User Input And Output
19 Sep, 2006 - 01:58 PM
Post #7

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,230



Thanked: 40 times
Dream Kudos: 25
My Contributions
You haven't even done step 1 yet...this appears to simply be a template.
User is offlineProfile CardPM
+Quote Post

cipherence
RE: User Input And Output
19 Sep, 2006 - 05:15 PM
Post #8

D.I.C Regular
Group Icon

Joined: 1 Apr, 2006
Posts: 260



Thanked: 1 times
Dream Kudos: 650
My Contributions
now is this like a GUI or a console program, i know it sounds dum but ya never know, and it does seem to be a template. try like cout<<"question"; then cin>>answer; to request data and input from user.
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: User Input And Output
19 Sep, 2006 - 05:26 PM
Post #9

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,230



Thanked: 40 times
Dream Kudos: 25
My Contributions
I would wager to say that this is not a GUI...the code given is obviously for a console program, and GUIs are rarely given as beginner assignments.
User is offlineProfile CardPM
+Quote Post

csstarter9
RE: User Input And Output
20 Sep, 2006 - 04:44 AM
Post #10

New D.I.C Head
*

Joined: 18 Sep, 2006
Posts: 21


My Contributions
QUOTE(Amadeus @ 19 Sep, 2006 - 02:58 PM) *

You haven't even done step 1 yet...this appears to simply be a template.



I have written the program but i still can't quite seem to get it to compile and work. I still keep getting the 1 error message and i can't quite seem to see where it is:

CODE

#include <iostream>

using namespace std;

int main()

bool done;
int num1;
int num2;
int product;
int userChoice;

bool finished;

do
{
cout << "Enter <1> if you want to start over \n";
cin >> userChoice;

if (userChoice == 1)
{
finished = true;
}
else
{
finished = false;
}
}

done = false;

cout << " Welcome Kid! \n";
cout << " This program will help you study multiplication. \n";

cout << " Enter two numbers between 0 and 10: \n";

while (done == false)
{

cin >> num1;
cin >> num2;


if (num1 > 10)
{
done = false;
cout << " Numbers should be no greater than 10! \n";
cout << " Enter two numbers: \n";
}

else if (num2 > 10)
{
done = false;
cout << " Numbers should be no greater than 10! \n";
cout << " Enter two numbers: \n";
}
else
{
done = true;
}



}

done = false;

cout << "How much is " <<num1 << " times " <<num2 <<" ?\n";

while (done == false)
{
cin >> product;

if (product == (num1*num2))
{
done = true;
cout << " Very Good! \n";
}

else
{
done = false;
cout << " Incorrect. Please try again. \n";

}


}

while (finished == false);

;return 0;
}


edit: added [code] tags ~ jayman9

This post has been edited by jayman9: 20 Sep, 2006 - 07:36 AM
User is offlineProfile CardPM
+Quote Post

csstarter9
RE: User Input And Output
20 Sep, 2006 - 04:59 AM
Post #11

New D.I.C Head
*

Joined: 18 Sep, 2006
Posts: 21


My Contributions
QUOTE(Xing @ 18 Sep, 2006 - 06:22 AM) *

You are having extra
CODE
{
cout << i << " and done. \n";
}
}

return 0;
}
in the end. Remove it and your code will compile. Next time use code tags to display your code.




I have completed all the steps but still get an 1 error message when I try to compile. here is the code:
CODE

#include <iostream>

using namespace std;

int main()
{ //initialization of the variables

bool done;
int num1;
int num2;
int product;
int userChoice;

bool finished;

do
{
cout << "Enter <1> if you want to start over \n";
cin >> userChoice;

if (userChoice == 1)
{
finished = true;
}
else
{
finished = false;
}
}

done = false;

cout << " Welcome Kid! \n";
cout << " This program will help you study multiplication. \n";

//request two integer numbers from user

cout << " Enter two numbers between 0 and 10: \n";

while (done == false)
{

cin >> num1;
cin >> num2;


if (num1 > 10)
{
done = false;
cout << " Numbers should be no greater than 10! \n";
cout << " Enter two numbers: \n";
}

else if (num2 > 10)
{
done = false;
cout << " Numbers should be no greater than 10! \n";
cout << " Enter two numbers: \n";
}
else
{
done = true;
}



}

done = false;

cout << "How much is " <<num1 << " times " <<num2 <<" ?\n";

while (done == false)
{
cin >> product;

if (product == (num1*num2))
{
done = true;
cout << " Very Good! \n";
}

else
{
done = false;
cout << " Incorrect. Please try again. \n";

}


}

while (finished == false);

;return 0;
}

User is offlineProfile CardPM
+Quote Post

Xing
RE: User Input And Output
20 Sep, 2006 - 05:04 AM
Post #12

D.I.C Addict
Group Icon

Joined: 22 Jul, 2006
Posts: 723



Thanked: 2 times
Dream Kudos: 1575
My Contributions
Complete your do-while loop.
User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Reply to this topicStart new topic
Time is now: 12/5/08 02:40AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month