|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Trouble ending loop
I'm having trouble ending a loop that is call a multiplication function.
I want to repeat then end when I decide to end it, instead of doing a for loop When I took a C class (over a year ago now), we used an EOF but I don't have my old work with me to look it up. I tried using setting bool up and it works, until I type something other than 1 or 0. And I can't find a solution to stop anything other than 1 or 0 from being entered. I'd like to not have to be asked for another problem to appear, and be able to end the program whenever. Here's my code. If you have any tips on my function I'm very open to suggestions to make it better. Just trying to learn in my spare time. As of now the function works properly so I'm not spending anytime improving it. Code:
#include <iostream>
#include <math.h>
using namespace std;
int multiply()
{
srand( time(NULL));
int a;
int i = rand() % 10;
int j = rand() % 10;
int b=i*j;
cout<<""<<i<<"x"<<""<<j<<"=";
cin>>a;
if(a==b){
cout<<"That is correct!"<<endl;
}//end if
while(a!=b){
cout<<"Try again!"<<endl;
cout<<""<<i<<"x"<<""<<j<<"=";
cin>>a;
if(a==b){
cout<<"That is correct!"<<endl;
}//end if
}//end while
}//end multiply function
//begin main to call multiply
int main()
{
bool x=true;
while(x!=false){
cout<<"Type 0 to exit, or 1 to continue: ";
cin>>x;
multiply();
}//end while
system("pause");
return 0;
}//end main
Thanks in advance to anyone who can give me a hint at what to do. ![]() |
|
#2
|
||||
|
||||
|
To begin with, better indentation would be a good move.
It's less than 50 lines, and already it's much harder to read than it needs to be. Follow the //!! comments for more ideas Code:
#include <iostream>
#include <cmath> //!! was math.h
using namespace std;
int multiply()
{
srand( time(NULL)); //!! you should do this just ONCE in main().
//!! if this were in a loop, you'd really notice the non-randomness
int a;
int i = rand() % 10;
int j = rand() % 10;
int b=i*j;
cout<<""<<i<<"x"<<""<<j<<"=";
cin>>a;
if(a==b){
cout<<"That is correct!"<<endl;
}//end if
while(a!=b){
cout<<"Try again!"<<endl;
cout<<""<<i<<"x"<<""<<j<<"=";
cin>>a;
if(a==b){
cout<<"That is correct!"<<endl;
}//end if
}//end while
}//end multiply function
//begin main to call multiply
int main()
{
bool x=true;
while(x!=false){
cout<<"Type 0 to exit, or 1 to continue: ";
cin>>x;
if ( cin.fail() ) {
//!! do something here
}
multiply();
}//end while
system("pause");
return 0;
}//end main
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut. |
|
#3
|
|||
|
|||
|
Quote:
Thank you very much. I was planning on using cmath originally, but couldn't remember if it was just math or cmath. and I will move srand to main as well. |
|
#4
|
||||
|
||||
|
Quote:
rand() and srand() are declared in <cstdlib>/<stdlib.h> which you did not include. It worked more by luck than judgement because presumably in your specific compiler's library, cstdlib is included by one of the other headers you used, but that is not a given for any compiler. Don't unnecessarily re-quote entire posts - it is already there for us to see. |
|
#5
|
|||
|
|||
|
I took out math.h after my last post yesterday, because I realized I didn't need it.
I don't know all the libraries and what's inside of them by heart, so forgive me for doing something by mistake. I have to learn somehow. Also, if I remember right, most of stdlib.h is covered by iostream. That's why system("pause") works without stdlib.h at least. |
|
#6
|
|||
|
|||
|
Quote:
|
|
#7
|
||||
|
||||
|
Quote:
Maybe on your compiler alone. It is not necessarily portable, so it may not work on another compiler. By the way, you should really include <cstdlib> instead of stdlib.h, since you're going the C++ way .Including it twice won't cost anything. C++ include files have header guards something like this: Code:
#ifndef CSTDLIB_H #define CSTDLIB_H ... rest of stuff #endif Thus including the file multiple times won't matter, because the preprocessor will only grab one copy anyway because of the include guards
__________________
Up the Irons What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home. "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest Down with Sharon Osbourne Puzzle of the Month solved by Fishmonger, superior perl programmer of the month |
|
#8
|
|||
|
|||
|
Ok. I thought it may have been a universal thing. My teacher wasn't very good when I took c and c++ and we really didn't cover much. So I don't have a lot of great information on different compilers and what not.
I've been using bloodshed's Dev-C++, and it's always worked. From now on I will include either <stdlib.h> or <cstdlib>. Thanks for the info. |
|
#9
|
|||
|
|||
|
Have it working the way I wanted it to now
Thanks for all the help. If anyone sees an area to improve on please let me know as I am going to be building off of this to slowly make a math tutoring program for simple math (starting with questions and answers, maybe moving on to explanations.)
Code:
#include <iostream>
#include <cstdlib>
using namespace std;
int add()
{
int a;
int i = rand() % 9;
int j = rand() % 9;
int b=i+j;
cout<<""<<i<<"+"<<""<<j<<"=";
cin>>a;
if(a!=b){
cout<<"Try again!"<<endl;
cout<<""<<i<<"+"<<""<<j<<"=";
cin>>a;
}//end if try again
if(a!=b){
cout<<"Try again!"<<endl;
cout<<""<<i<<"+"<<""<<j<<"=";
cin>>a;
}//end if try again
if(a==b){
cout<<"That is correct!"<<endl<<endl;
}//end if correct
if(a!=b){
cout<<"The correct answer is "<<b<<endl<<endl;
}//end if answer
}//end addition function
//begin main to call addition
int main()
{
srand( time(NULL));
bool x=true;
while(x==true){
cout<<"Type 0 to exit, or 1 to continue: ";
cin>>x;
add();
}//end while
system("pause");
return 0;
}//end main
|
|
#10
|
||||
|
||||
|
Yes, just one thing. Instead of importing everything in the std; namespace, it is better to qualify them.
That is, instead of doing this: Code:
using namespace std; cout << "Hello world\n"; cin >> foo; it is better to do this: Code:
// using namespace std; <-- Notice we commented out this line std::cout << "Hello world\n"; std::cin >> foo; or if you don't feel like typing std:: in front of each of your standard objects, at least qualify only specific objects instead of everything in your std:: namespace. Code:
using namespace std::cout; using namespace std::cin; cout << "Hello world\n"; cin >> foo; The whole purpose of namespaces is to prevent name conflicts. By using namespace std; you're negating the entire reason for namespaces. See http://www.parashift.com/c++-faq-li...s.html#faq-27.5 for more. |
|
#11
|
|||
|
|||
|
Ok thank you very much for the info. I never really understood the difference, but that makes sense.
|
|
#12
|
||||
|
||||
|
Quote:
It is very straightforward; if you use a function or class, include its header; even if it may be included indirectly in some other way. The point about indirect inclusion is that under code maintenance or porting to a different platform or compiler, such inclusion may not always hold. If you don't know what header a function or class belongs to, look it up in an appropriate reference. Good references include http://www.cppreference.com and http://cplusplus.com/reference/. You don't need an exhaustive knowledge of the standard library in your head to use it effectively. You can get a general overview of its capabilities in a couple of hours. You will then be able to apply it appropriately more often, using the references for the detail. Quote:
Clifford Last edited by clifford : June 26th, 2009 at 03:57 PM. Reason: fixed bad link |
|
#13
|
|||
|
|||
|
Ah I see. I will have to go through the libraries and see what's included where when I get a chance.
Do you have a suggestion for a better compiler by chance then? My school has always used Dev-c++. I seem to have received a lot of bad information from my school though when it comes to programming. Another good reason for trying to go back and learn on my own. Thanks for the information on the libraries, I appreciate it. |
|
#14
|
||||
|
||||
|
Quote:
If you continue to use Dev-C++ at least make sure you are using version 4.9.9.2. Otherwise: http://www.microsoft.com/Express/vc/ Clifford |
|
#15
|
|||
|
|||
|
OK sounds good. The teacher teaching c and c++ at my school is still semi stuck in the days of punch cards, so that could be her main issue.
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > Trouble ending loop |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|