Welcome to the North American Subaru Impreza Owners Club Friday December 26, 2014
Home Forums WikiNASIOC Products Store Modifications Upgrade Garage
NASIOC
Here you can view your subscribed threads, work with private messages and edit your profile and preferences Home Registration is free! Visit the NASIOC Store NASIOC Rules Search Find other members Frequently Asked Questions Calendar Archive NASIOC Upgrade Garage Logout
Go Back   NASIOC > NASIOC Miscellaneous > Off-Topic

Welcome to NASIOC - The world's largest online community for Subaru enthusiasts!
Welcome to the NASIOC.com Subaru forum.

You are currently viewing our forum as a guest, which gives you limited access to view most discussions and access our other features. By joining our community, free of charge, you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is free, fast and simple, so please join our community today!

If you have any problems with the registration process or your account login, please contact us.
* Registered users of the site do not see these ads.
Reply
 
Thread Tools Display Modes
Old 10-20-2004, 01:41 PM   #1
STIgeorge
Scooby Specialist
 
Member#: 22040
Join Date: Jul 2002
Chapter/Region: South East
Location: Columbia, SC
Vehicle:
12 FJ Cruiser
Rent-a-car White

Default Yet another C++ pickle.

Writing a program that teaches multiplication:
Use srand to produce two positive one-digit integers: How much is 6 times 7? If the student types in wrong answer, say it is incorrect and repeat question giving student 3 guesses before printing correct answer. If its correct, say so and move on to next question.
Program should prompt user, Continue? (Y/N), after every set of 5 questions. Print out in the end how many the user got wrong.

The cout stuff I'm fine with, its just the use of loops thats totally throwing me off. I know I need 3 loops, one to run the Y/N question, one to run the 5 problems and one to run the 3 tries, but I havent a clue on how to set them up.

This is what I have so far:

# include <iostream>
# include <cstdlib>
using namespace std;

void main () {
srand(0);
int num1, num2, answer;
char y,n [2];
int tries;

num1 = rand()% 10+1;
num2 = rand()% 10+1;
answer =(num1*num2);



for(tries =1; tries <=2; tries ++) {
cout << "Would you like 5 problems?\n\n";
if (y)

Any hints would be greatly appreciated.

Im also happy to say that I'm switching my major from Mech Eng, I just need to get through this semester's classes.

Thanks a bunch,
George
* Registered users of the site do not see these ads.

Last edited by STIgeorge; 10-20-2004 at 01:46 PM.
STIgeorge is offline   Reply With Quote
Old 10-20-2004, 01:44 PM   #2
Philsdabom
Scooby Newbie
 
Member#: 55288
Join Date: Feb 2004
Chapter/Region: MAIC
Location: Springfield, VA
Default

I like dill pickles
Philsdabom is offline   Reply With Quote
Old 10-20-2004, 01:46 PM   #3
AccidenT
Scooby Specialist
 
Member#: 2024
Join Date: Aug 2000
Chapter/Region: NESIC
Location: South Portland, ME
Vehicle:
2005 Legacy 2.5i
Atlantic Blue Pearl

Default

The key is the loop heirarchy. Here's a hint on the order, but you'll have to do the conditions yourself

Code:
<loop conditions for y/n question> {

   <loop conditions for 5 questions> {

       <loop conditions for 3 tries> {
       
        }

   }

}
AccidenT is offline   Reply With Quote
Old 10-20-2004, 01:48 PM   #4
STIgeorge
Scooby Specialist
 
Member#: 22040
Join Date: Jul 2002
Chapter/Region: South East
Location: Columbia, SC
Vehicle:
12 FJ Cruiser
Rent-a-car White

Default

Quote:
Originally Posted by AccidenT
The key is the loop heirarchy. Here's a hint on the order, but you'll have to do the conditions yourself

Code:
<loop conditions for y/n question> {

   <loop conditions for 5 questions> {

       <loop conditions for 3 tries> {
       
        }

   }

}

DUH! Haha, I know the heirarchy. It just so happens to be the conditions that are throwing me off. Oh well, back to it until the Real Estate Lady comes to check out the house anyway. Maybe she'll be hot.

And after the first "for" statement, I need to put an "if" to make it kick me out of that loop if the answer is no, correct?

-Geo
STIgeorge is offline   Reply With Quote
Old 10-20-2004, 01:51 PM   #5
AccidenT
Scooby Specialist
 
Member#: 2024
Join Date: Aug 2000
Chapter/Region: NESIC
Location: South Portland, ME
Vehicle:
2005 Legacy 2.5i
Atlantic Blue Pearl

Default

Quote:
Originally Posted by STIgeorge
DUH! Haha, I know the heirarchy. It just so happens to be the conditions that are throwing me off. Oh well, back to it until the Real Estate Lady comes to check out the house anyway. Maybe she'll be hot.

And after the first "for" statement, I need to put an "if" to make it kick me out of that loop if the answer is no, correct?

-Geo

Well based on this part of your code, it seemed clear you were confusing the loops.

Quote:
for(tries =1; tries <=2; tries ++) {
cout << "Would you like 5 problems?\n\n";
if (y)
You're asking the question about 5 problems after each attempt at each question, instead of once every five questions.


Another hint: you'll need one while loop.
AccidenT is offline   Reply With Quote
Old 10-20-2004, 02:15 PM   #6
Asinine
Sufficiently Sharky
 
Member#: 55173
Join Date: Feb 2004
Chapter/Region: MAIC
Location: I beat up big rocks
Vehicle:
with paddles.

Default

I think you need to write this in psuedocode first. Get the logic down, and then write code to match your logic. One mistake code-newbies make is to try to write the code top to bottom. You can do that when you're much more proficient. For now, create the logical framework for your program. The syntax comes later... that's the easy part.
Asinine is offline   Reply With Quote
Old 10-20-2004, 02:20 PM   #7
FuzzyNips
Scooby Newbie
 
Member#: 16686
Join Date: Mar 2002
Chapter/Region: MWSOC
Location: Warrenville, IL
Vehicle:
2010 Mazda 3s GT 5dr
Black Mica

Default

Great suggestion Asinine. Similarly, once you find yourself working on larger projects, it will be to your advantage to write sections at a time and make sure everything you have is working as it should. If you sit down and try to do it all at once you'll find yourself getting overwhelmed very easily. These are things I'm just learning now (second year CS major) and they've helped me greatly.
FuzzyNips is offline   Reply With Quote
Old 10-20-2004, 02:21 PM   #8
STIgeorge
Scooby Specialist
 
Member#: 22040
Join Date: Jul 2002
Chapter/Region: South East
Location: Columbia, SC
Vehicle:
12 FJ Cruiser
Rent-a-car White

Default

I actually alread have written down everything, my prof said I was one of the first newbs that she's ever seen doing that from the get-go. (Go me!). Even with the way I want it to turn out written in front of me, I have no idea what expressions to use to get the desired results.
STIgeorge is offline   Reply With Quote
Old 10-20-2004, 02:36 PM   #9
Asinine
Sufficiently Sharky
 
Member#: 55173
Join Date: Feb 2004
Chapter/Region: MAIC
Location: I beat up big rocks
Vehicle:
with paddles.

Default

Start program.
Declare ints for counter for wrong answers, counter 0-5, 1st number and for 2nd number, student answer
Declare Boolean for continuing = true.
Test boolean and begin while loop.
Loop to 5
..Generate randoms
..ask for input
..compare answer to randxrand
..take appropriate action
..end loop to 5
Ask to continue
get input
check to see if input is valid
set boolean to input
end while loop

That's psuedocode... I left the tricky part in italics up to you.
Asinine is offline   Reply With Quote
Old 10-20-2004, 02:36 PM   #10
Asinine
Sufficiently Sharky
 
Member#: 55173
Join Date: Feb 2004
Chapter/Region: MAIC
Location: I beat up big rocks
Vehicle:
with paddles.

Default

And furthermore, tell me what some punk ass student is doing driving around an STi, while I'm in a WRX?!
Asinine is offline   Reply With Quote
Old 10-20-2004, 02:38 PM   #11
tux
Scooby Newbie
 
Member#: 40949
Join Date: Jul 2003
Chapter/Region: Tri-State
Location: nj, usa
Vehicle:
2003 wrx
wr blue

Default

recursion and classes

and pseudo code is for weenies... just hop in... also don't forget to put 1 function per cpp file... and tons of .h files

make sure for your prof to find out what you are doing at least 30 copies of notepad must be open to follow your insideous trail of code running rampant all over the place

and don't forget you can easily call .asm files... so do that also!

make it obscure so nobody even wants to look at your code and you get an A for being evil

that's how i got through data structures and algorithm analysis... although I never used a pointer... teach probably got sick of going on a wild goose hunt.

pointers are the devil and should never be used EVER!

stacks are bad too.. make sure everything is in an array or matrix

and help is bad... unless you work at a company that has tons of developers all the better. but anyways i'm switching from comp sci to mechanical engineerings.... more money there

i see programmers out on the streets all the time outside of burlington coat factory with dirty cardboard signs that Say "Will Right Code 4 Food" and even more often do I see project managers next to them with signs saying "Will Order Around Code Monkee for more Food"

Last edited by tux; 10-20-2004 at 02:43 PM.
tux is offline   Reply With Quote
Old 10-20-2004, 02:40 PM   #12
Asinine
Sufficiently Sharky
 
Member#: 55173
Join Date: Feb 2004
Chapter/Region: MAIC
Location: I beat up big rocks
Vehicle:
with paddles.

Default

Quote:
Originally Posted by tux
recursion and classes

and pseudo code is for weenies... just hop in... also don't forget to put 1 function per cpp file... and tons of .h files

make sure for your prof to find out what you are doing at least 30 copies of notepad must be open to follow your insideous trail of code running rampant all over the place

and don't forget you can easily call .asm files... so do that also!

make it obscure so nobody even wants to look at your code and you get an A for being evil

that's how i got through data structures and algorithm analysis... although I never used a pointer... teach probably got sick of going on a wild goose hunt.

pointers are the devil and should never be used EVER!
He's joking.... he's just joking
Asinine is offline   Reply With Quote
Old 10-20-2004, 02:46 PM   #13
tux
Scooby Newbie
 
Member#: 40949
Join Date: Jul 2003
Chapter/Region: Tri-State
Location: nj, usa
Vehicle:
2003 wrx
wr blue

Default

no listen to me

i'm correct!
tux is offline   Reply With Quote
Old 10-20-2004, 02:48 PM   #14
tux
Scooby Newbie
 
Member#: 40949
Join Date: Jul 2003
Chapter/Region: Tri-State
Location: nj, usa
Vehicle:
2003 wrx
wr blue

Default

and today i may have an sti :P

or maybe saturday.. depends on if i get out of school on time tonight or have enough time to stop at the dealer after work tomorrow

100% buy back on my wrx!
tux is offline   Reply With Quote
Old 10-20-2004, 03:00 PM   #15
FuzzyNips
Scooby Newbie
 
Member#: 16686
Join Date: Mar 2002
Chapter/Region: MWSOC
Location: Warrenville, IL
Vehicle:
2010 Mazda 3s GT 5dr
Black Mica

Default

I wrote it pretty quickly so line spacing looks bad but the functionality that you're looking for should be there. I didn't want to do your assignment for you but pay attention to the comments to get a better idea of how things are working. I am by no means a C++ expert and I'm sure there are a lot better ways of writing this program, but it should give you some help (which was my intended goal):

Code:
// preprocessor diretives
#include <iostream.h>

// standard namespace
using namespace std;

// main
int main()
{
	// variable declarations
	int num1, num2, answer;
	int userAnswer;
	char again;
	
	// random generator seed
	srand(0);

	// while loop that executes as long as the user wishes to continue
	do
	{
		// variable declarations for each set of 5 problems
		int problem = 0;		// problem number
		int numCorrect = 0;		// number of correct answers
		
		// loops through the 5 problems	   
		while (problem < 5)
		{	 
			// generates the 2 random numbers
			num1 = rand()% 10+1;
			num2 = rand()% 10+1;
			
			// calculates the answer
			answer =(num1*num2);
			
			// displays question and waits for user input
			cout << "How much is " << num1 << " times " << num2 << "? ";
			cin >> userAnswer;
			
			// if answer is wrong	 	 
			if (userAnswer != answer)
			{
				// number of tries for current incorrect answer
				int tries = 0;
				
				// loops through giving the user a maximum of 3 guesses
				while (tries < 3)
				{
					// displays incorrect answer message
					cout << userAnswer << " is incorrect, you have " << 3 - tries << " more tries.";
					cout << endl;
					
					// prompts user to input their new answer
					cout << "Please enter your new answer: ";
					cin	>> userAnswer;
					
					// if new answer is correct	   	   	   	   
					if (userAnswer == answer)
					{
						// increments number of correct answers
						numCorrect++;
						
						// displays correct answer message
						cout << "That is correct!" << endl;
						
						// breaks out of while loop that gives up to 3 guesses	  
						break;
					}
					// if new answer is incorrect
					else
					{
						// increments tries
						tries++;
						
						// lets user know they're out of attempts and still didn't get
						// the question right...moving on to next question
						if (tries == 3)
						{
							cout << userAnswer << " is incorrect, you are out of tries,";
							cout << " moving on to the next question." << endl;  
						}
					}
				}	
			}
			// original answer is correct
			else
			{
				// increments number of correct answers
				numCorrect++;
				
				// displays correct answer message
				cout << "That is correct!" << endl;
			}
			
			// increments problem 	  	  
			problem++;
		}
		
		// displays final total of correct answers out of 5 	
		cout << endl << "You got " << numCorrect << " out of 5 correct!" << endl << endl; 
		
		// prompts user to continue or exit program
		cout << "Would you like to continue? (y/n) ";
		cin >> again;
	} while (again == 'y' || again == 'Y');
}
I displayed number of correct answers(to make the user feel good) but if you want number wrong then make the necessary changes. (Or if you're lazy then just make it print out (5 - numCorrect)).
FuzzyNips is offline   Reply With Quote
Old 10-20-2004, 03:06 PM   #16
tux
Scooby Newbie
 
Member#: 40949
Join Date: Jul 2003
Chapter/Region: Tri-State
Location: nj, usa
Vehicle:
2003 wrx
wr blue

Default

wow you have got quite a many if statements... but that looks to be about right for someone 1st or 2nd year to understand mad props!

i'm not helpful at all with c++ because either i do the whole thing for someone or they can't read it ;D
tux is offline   Reply With Quote
Old 10-20-2004, 03:07 PM   #17
FuzzyNips
Scooby Newbie
 
Member#: 16686
Join Date: Mar 2002
Chapter/Region: MWSOC
Location: Warrenville, IL
Vehicle:
2010 Mazda 3s GT 5dr
Black Mica

Default

I wrote it really quickly so like I said, there probably are a lot more efficient ways to do it...oh well!
FuzzyNips is offline   Reply With Quote
Old 10-20-2004, 03:21 PM   #18
tux
Scooby Newbie
 
Member#: 40949
Join Date: Jul 2003
Chapter/Region: Tri-State
Location: nj, usa
Vehicle:
2003 wrx
wr blue

Default

easy to read I'd give you an A-

A+ is withheld for students that use .h and other .cpp and .asm files attached... also many many functions and obscure code
tux is offline   Reply With Quote
Old 10-20-2004, 03:26 PM   #19
FuzzyNips
Scooby Newbie
 
Member#: 16686
Join Date: Mar 2002
Chapter/Region: MWSOC
Location: Warrenville, IL
Vehicle:
2010 Mazda 3s GT 5dr
Black Mica

Default

FuzzyNips is offline   Reply With Quote
Old 10-20-2004, 03:36 PM   #20
Asinine
Sufficiently Sharky
 
Member#: 55173
Join Date: Feb 2004
Chapter/Region: MAIC
Location: I beat up big rocks
Vehicle:
with paddles.

Default

Good effort Nips.

1. Don't use a break; statement... it's considered bad form. Instead, set up your condition to be while(tries < 3 && wrongAnswerBoolenHere)

2. You're supposed to keep track of wrong answers, right?

3. It may not be required, but type checking the y/n could be good for bonus points

4. rand() % 10 + 1 should be (rand() % 9) + 1 or else you may end up with "10" as one of your operands, and that's a no-no... and parentesis are your friends
Asinine is offline   Reply With Quote
Old 10-20-2004, 03:41 PM   #21
FuzzyNips
Scooby Newbie
 
Member#: 16686
Join Date: Mar 2002
Chapter/Region: MWSOC
Location: Warrenville, IL
Vehicle:
2010 Mazda 3s GT 5dr
Black Mica

Default

#1 is helpful for me, #2 I just decided to do correct answers, #3 I don't think I've learned yet (or I forgot it), and #4 all I have to say is "oops." :lol

Usually I do a better job with the parenthesis but I did some speed coding.
FuzzyNips is offline   Reply With Quote
Old 10-20-2004, 03:45 PM   #22
STIgeorge
Scooby Specialist
 
Member#: 22040
Join Date: Jul 2002
Chapter/Region: South East
Location: Columbia, SC
Vehicle:
12 FJ Cruiser
Rent-a-car White

Default

I ended up taking a break. With lunch down and a shower, I should be able to hack this out now.
Thanks guys
George
STIgeorge is offline   Reply With Quote
Old 10-20-2004, 03:52 PM   #23
FuzzyNips
Scooby Newbie
 
Member#: 16686
Join Date: Mar 2002
Chapter/Region: MWSOC
Location: Warrenville, IL
Vehicle:
2010 Mazda 3s GT 5dr
Black Mica

Default

Does someone want to do my program?
FuzzyNips is offline   Reply With Quote
Old 10-20-2004, 04:01 PM   #24
2k2
NASIOC Supporter
 
Member#: 12368
Join Date: Nov 2001
Chapter/Region: MAIC
Location: Pittsburgh,PA
Vehicle:
2008 Infiniti G35X
2011 BMW X5 Diesel

Default

Fuzzy I did enough for you!!

Btw your teacher is a moron...

Oh yeah and get .NET so it makes it EASIER!!!

oh yeah... I forgot to mention... a few loops and your done... id type if for you but fuzzy did all the hard work.
2k2 is offline   Reply With Quote
Old 10-20-2004, 04:08 PM   #25
FuzzyNips
Scooby Newbie
 
Member#: 16686
Join Date: Mar 2002
Chapter/Region: MWSOC
Location: Warrenville, IL
Vehicle:
2010 Mazda 3s GT 5dr
Black Mica

Default

LoL 2k2BlackWRX...I got a copy of VS 6.0 from a kid today...
FuzzyNips is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Yet another, another, fog light question. 97OBLimited Electrical & Lighting 0 04-13-2010 11:40 PM
Yet another reason I'll never buy another domestic.... f4phantomii Off-Topic 62 04-04-2010 08:58 PM
Yet another another speeding ticket Q.. ebrake_n North West Impreza Club Forum -- NWIC 10 03-22-2009 02:38 AM
Yet another threat about yet another boring work day Mad06STi Off-Topic 5 10-04-2007 12:48 PM
('93-'01) Yet another Japanese translation... Please! :) MrP0nch0 Impreza Forum 7 06-14-2001 03:41 PM


All times are GMT -4. The time now is 01:36 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Powered by Searchlight © 2014 Axivo Inc.
Copyright ©1999 - 2014, North American Subaru Impreza Owners Club, Inc.