Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

C++ Help with practice problem


  • Please log in to reply
10 replies to this topic

#1 thisisu

thisisu

  • Malware Response Team
  • 2,525 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:USA
  • Local time:06:22 PM

Posted 30 October 2012 - 06:52 PM

Currently reading this book: http://www.cprogramming.com/c++book/?inl=ft-nhp

So far it's pretty good and I have been able to follow along quite nicely until now. Stuck on this practice problem which states:

Ask the user for two users' ages, and indicate who is older; behave differently if someone is over 100.


Here is what I have so far (doesn't work):

#include <iostream>

using namespace std;

int main ()
{
	int jane;
	int doe;

	cout << "First, how old is Jane?\n";
	cin >> jane; // prompt user for Jane's age

	cout << "Second, how old is Doe?\n";
	cin >> doe; // prompt user for doe's age

	if ( ( jane || doe ) >= 100 ) // Book says >= takes precedence over || (OR)
	{
		cout << "Jane or Doe is at least 100 years old!";
		cin.get(); // pause
	}
	else
	{
        // Second test of who is older will be here
	}
}

Can someone please help? :o

Edited by thisisu, 30 October 2012 - 06:54 PM.


BC AdBot (Login to Remove)

 


#2 DarkSnake-Kobra

DarkSnake-Kobra

  • Members
  • 633 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Iowa, USA
  • Local time:07:22 PM

Posted 30 October 2012 - 08:16 PM

Hi thisisu :)


I have a little experience in C++ and I did something like this in my C++ book which used the OR ELSE operator and used two if statements rather then one. I've been slacking a bit working on what not so I could use some freshening up.

Edit: Been so long I'd have to read the book again. I'm having a complete brain fart here. :wacko:

Edited by DarkSnake-Kobra, 30 October 2012 - 08:32 PM.


#3 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer


  • Malware Response Team
  • 12,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:05:22 PM

Posted 31 October 2012 - 01:04 AM

Probably more than any other language (except PHP), there are a LOT of bad C++ books in print. I'd start here for a list of good ones: http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list

Billy3
Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image

#4 thisisu

thisisu
  • Topic Starter

  • Malware Response Team
  • 2,525 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:USA
  • Local time:06:22 PM

Posted 31 October 2012 - 01:50 AM

Hi DarkSnake-Kobra and Billy :)

Bookmarked that link, Billy, thanks.

Just out of curiosity, can you help me solve the above problem?

#5 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer


  • Malware Response Team
  • 12,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:05:22 PM

Posted 31 October 2012 - 12:34 PM

What do you mean "doesn't work"?

What is the expected behavior?

What is the behavior you're actually getting?

Have you tried looking at the execution of the program in a debugger?

Billy3
Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image

#6 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:06:22 PM

Posted 31 October 2012 - 04:24 PM

What do you mean "doesn't work"?

What is the expected behavior?

What is the behavior you're actually getting?


Arguably the three most important questions in a bug report. Nobody ever seems to get it right.

#7 thisisu

thisisu
  • Topic Starter

  • Malware Response Team
  • 2,525 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:USA
  • Local time:06:22 PM

Posted 01 November 2012 - 12:43 AM

That's odd... now it is working. :wacko:
Before it was saying that someone was always over 100 years old. Maybe I had > and < mixed up? :lol:

Oh well, ending code (yay!) :)

#include <iostream>

using namespace std;

int main ()
{
        int jane;
        int doe;

        cout << "First, how old is Jane?\n";
        cin >> jane; // prompt user for Jane's age

        cout << "Second, how old is Doe?\n";
        cin >> doe; // prompt user for doe's age

        if ( ( jane || doe ) >= 100 ) // Book says >= takes precedence over || (OR)
        {
                cout << "Jane or Doe is at least 100 years old!";
                cin.get(); // pause
        }
        else if ( jane > doe )
		{
			cout << "Jane is older than Doe";
			cin.get(); // pause
        }
		else if ( doe > jane )
		{
			cout << "Doe is older than Jane";
			cin.get(); // pause
		}
}


#8 DarkSnake-Kobra

DarkSnake-Kobra

  • Members
  • 633 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Iowa, USA
  • Local time:07:22 PM

Posted 01 November 2012 - 12:00 PM

Looks good! I edited the code a bit though to fill in the gap on if they were the same age.

#include <iostream>

using namespace std;

int main ()
{
        int jane;
        int doe;

        cout << "First, how old is Jane?\n";
        cin >> jane; // prompt user for Jane's age

        cout << "Second, how old is Doe?\n";
        cin >> doe; // prompt user for doe's age

        if ( ( jane || doe ) >= 100 ) // Book says >= takes precedence over || (OR)
        {
                cout << "Jane or Doe is at least 100 years old!";
                cin.get(); // pause
        }
        else if ( jane > doe )
                {
                        cout << "Jane is older than Doe";
                        cin.get(); // pause
        }
                else if ( doe > jane )
                {
                        cout << "Doe is older than Jane";
                        cin.get(); // pause
                }

                else if ( doe == jane) //order does not matter here
                cout << "They are the same age!";
}

You may also want to add something at the end to ensure the user sees the result.

#include <iostream>

using namespace std;

int main ()
{
        int jane;
        int doe;

        cout << "First, how old is Jane?\n" << endl;
        cin >> jane; // prompt user for Jane's age

        cout << "Second, how old is Doe?\n" << endl;
        cin >> doe; // prompt user for doe's age

        if ( ( jane || doe ) >= 100 ) // Book says >= takes precedence over || (OR)
        {
                cout << "Jane or Doe is at least 100 years old!" << endl;
                                cin.get(); // pause
        }
        else if ( jane > doe )
                {
                        cout << "Jane is older than Doe" << endl;
                        cin.get(); // pause
        }
                else if ( doe > jane )
                {
                        cout << "Doe is older than Jane" << endl;
                        cin.get(); // pause
                }

                else if ( doe == jane) //order does not matter here
                cout << "They are the same age!" << endl;
                char response;
                cout << endl << "Type close and press enter to exit the program" << endl << endl;;
                cin >> response;
                return 0;
}

char response;
cout << endl << "Type close and press enter to exit the program" << endl << endl;;
cin >> response;
return 0;


This makes the user input something before the program closes that way you know they saw the results. It's also a bit cleaner and easier to read with line breaks. Technically they can type whatever they want and press enter to close the program, but I think it's a bit better to make them type something as it's more straightforward and less confusing.

Edited by DarkSnake-Kobra, 01 November 2012 - 12:35 PM.


#9 thisisu

thisisu
  • Topic Starter

  • Malware Response Team
  • 2,525 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:USA
  • Local time:06:22 PM

Posted 03 November 2012 - 08:26 PM

:thumbsup:

#10 kipl09

kipl09

  • Members
  • 4 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:india
  • Local time:07:22 PM

Posted 24 November 2012 - 06:28 AM

You can use this logic for solve your problem.
int main ()
{
int a;
int b;

cout << "Enter the age of A\n";

cin >> a;

cout << “Enter the age of B\n";

cin >> b;

if ( a>b & a<=100 )

cout << “A is greatest Then B\n";

else if ( b>a & b<=100 )

cout << “B is greatest Then A\n";

else if( a>100 )

cout << “A At Least 100 years old”;

else

cout << “B At Least 100 years old”;

}
return 0;

Edited by groovicus, 24 November 2012 - 10:04 AM.


#11 thisisu

thisisu
  • Topic Starter

  • Malware Response Team
  • 2,525 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:USA
  • Local time:06:22 PM

Posted 24 November 2012 - 08:31 PM

Thank you kipl09 and groovicus :thumbup2:




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users