Omnimaga
General Discussion => Technology and Development => Computer Programming => Topic started by: ruler501 on March 07, 2011, 08:34:15 am
-
I am using Microsoft Visual C++ 2010 and when I compile thic code. It then says running for about a minute then goes away. I never see anything from my program run. what am I doing wrong this has never happened to me before while I was using this software.
This is my code for a MAP style test in math
/* rand example: guess the number */
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
int randomgen(int range, int bottom);
int addition();
int subtraction();
int multiplication();
int division();
int score=0;
int main()
{
srand(time(NULL));
int randint, wrongs=0;
for(int i; i<20; i++)
{
if(score<3)
{
randint=randomgen(6,0);
if(randint==5)
{
wrongs+=addition();
}
else if(randint==4)
{
wrongs+=subtraction();
}
else if(randint==3)
{
wrongs+=multiplication();
}
else if(randint==2)
{
wrongs+=division();
}
/* else if(randint==1)
{
wrongs+=simplelinear();
}
else
{
randint=randomgen(3,0);
if(randint==0)
{
wrongs+=determinant();
}
else if(randint==1)
{
wrongs+=scalarmultiplication();
}
else
{
wrongs+=cts();
}
}*/
}
}
system("PAUSE");
}
//Function Definitions
int randomgen(int range, int bottom)
{
int iSecret;
iSecret = rand()%range+bottom;
return iSecret;
}
int addition()
{
int numone, numtwo, answer;
numone=randomgen(900, 100);
numtwo=randomgen(600, 50);
cout << "What is %d + %d equal ", numone, numtwo;
cin >> answer;
cout << "\n";
if(answer==numone+numtwo)
{
score++;
return 0;
}
else
{
return 1;
}
}
int subtraction()
{
int numone, numtwo, answer;
numone=randomgen(200, 799);
numtwo=randomgen(800, 1);
cout << "What is %d - %d equal ", numone, numtwo;
cin >> answer;
cout << "\n";
if(answer==numone-numtwo)
{
score++;
return 0;
}
else
{
return 1;
}
}
int multiplication()
{
int numone, numtwo, answer;
numone=randomgen(900, 100);
numtwo=randomgen(750, 50);
cout << "What is %d * %d equal ", numone, numtwo;
cin >> answer;
cout << "\n";
if(answer==numone+numtwo)
{
score+=2;
return 0;
}
else
{
return 1;
}
}
int division()
{
int numone, numtwo, answer;
numtwo=randomgen(350, 50);
numone=numtwo*randomgen(900, 100);
cout << "What is %d / %d equal ", numone, numtwo;
cin >> answer;
cout << "\n";
if(answer==numone/numtwo)
{
score+=2;
return 0;
}
else
{
return 1;
}
}
It also shows these errors when the code is running
'MAP.exe': Loaded 'E:\MAP\MAP\Debug\MAP.exe', Symbols loaded.
'MAP.exe': Loaded 'C:\WINDOWS\system32\ntdll.dll', Cannot find or open the PDB file
The program '[8712] MAP.exe: Native' has exited with code -1073741790 (0xc0000022).
-
it doesn't pause after it finishes.
Source: 30 min of learning C last night :/
-
system("Pause") is supposed to pause the system
and I found out it was just my AntiVirus(Norton) blocking it.
The %d is not working to add in the variable names how do I do that another way?
-
hmm, the %d should be working. I would suggest using something like printf() (or C++'s equivalent, forgot the library it's in) to do that. I'm not sure if cout accepts it in that format.
-
right now I am using this as a work around
cout << "What is" << numone << "+" numtwo << "equal"
-
that should work just fine, though I still advise a formatted print function that does the work for you, it saves space, time, and just easier to use and less stupid mistakes will be made.
-
I'm not sure how to I just taught myself most of this a little while ago. Any help would be appreciated
EDIT: Here is my newer code
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
int randomgen(int range, int bottom);
int addition();
int subtraction();
int multiplication();
int division();
int score=0, wrongs=0;
int main()
{
srand(time(NULL));
int randint;
for(int i=0; i<20; i++)
{
if(score<3)
{
randint=randomgen(6,0);
if(randint==5)
{
wrongs+=addition();
}
else if(randint==4)
{
wrongs+=subtraction();
}
else if(randint==3)
{
wrongs+=multiplication();
}
else if(randint==2)
{
wrongs+=division();
}
/* else if(randint==1)
{
wrongs+=simplelinear();
}
else
{
randint=randomgen(3,0);
if(randint==0)
{
wrongs+=determinant();
}
else if(randint==1)
{
wrongs+=scalarmultiplication();
}
else
{
wrongs+=cts();
}
}*/
if(wrongs>=10)
{
break;
}
}
}
system("PAUSE");
}
//Function Definitions
int randomgen(int range, int bottom)
{
int iSecret;
iSecret = rand()%range+bottom;
return iSecret;
}
int addition()
{
int numone, numtwo, answer;
numone=randomgen(900, 100);
numtwo=randomgen(600, 50);
// cout << "What is %d + %d equal ", numone, numtwo; uncomment when figure out how to make it work
cout << "What is " << numone << "+" << numtwo << " equal ";
cin >> answer;
cout << "\n";
if(answer==numone+numtwo)
{
score++;
}
else
{
wrongs++;
}
}
int subtraction()
{
int numone, numtwo, answer;
numone=randomgen(200, 799);
numtwo=randomgen(800, 1);
cout << "What is " << numone << "-" << numtwo << " equal ";
cin >> answer;
cout << "\n";
if(answer==numone-numtwo)
{
score++;
}
else
{
wrongs++;
}
}
int multiplication()
{
int numone, numtwo, answer;
numone=randomgen(900, 100);
numtwo=randomgen(750, 50);
cout << "What is " << numone << "*" << numtwo << " equal ";
cin >> answer;
cout << "\n";
if(answer==numone+numtwo)
{
score+=2;
}
else
{
wrongs++;
}
}
int division()
{
int numone, numtwo, answer;
numtwo=randomgen(350, 50);
numone=numtwo*randomgen(900, 100);
cout << "What is " << numone << "/" << numtwo << " equal ";
cin >> answer;
cout << "\n";
if(answer==numone/numtwo)
{
score+=2;
}
else
{
wrongs++;
}
}
-
apparently kerm says that sprintf() is part of C++ (I wouold look up the library though) and that's quite useful. I suggest using that instead.
-
I will repost my code in a second. I redid it with printf instead of the couts
sprintf doesn't exist according to Dev-C++
Too many semicolons... I like python
Here is my code#include <iostream>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
int randomgen(int range, int bottom);
int addition();
int subtraction();
int multiplication();
int division();
int simplelinear();
int score=0, wrongs=0;
int main()
{
srand(time(NULL));
int randint;
for(int i=0; i<20; i++)
{
cout << score;
cout << "\n";
cout << i;
cout << "\n";
if(score<3)
{
randint=randomgen(6,0);
if(randint==5)
{
wrongs+=addition();
}
else if(randint==4)
{
wrongs+=subtraction();
}
else if(randint==3)
{
wrongs+=multiplication();
}
else if(randint==2)
{
wrongs+=division();
}
else if(randint==1)
{
wrongs+=simplelinear();
}
/* else
{
randint=randomgen(3,0);
if(randint==0)
{
wrongs+=determinant();
}
else if(randint==1)
{
wrongs+=scalarmultiplication();
}
else
{
wrongs+=cts();
}
}*/
if(wrongs>=10)
{
break;
}
}
}
system("PAUSE");
}
//Function Definitions
int randomgen(int range, int bottom)
{
int iSecret;
iSecret = rand()%range+bottom;
return iSecret;
}
int addition()
{
int numone, numtwo, answer;
numone=randomgen(900, 100);
numtwo=randomgen(600, 50);
printf("what is %d+%d ", numone, numtwo);
cin >> answer;
cout << "\n";
if(answer==numone+numtwo)
{
score++;
return 0;
}
else
{
wrongs++;
return 0;
}
}
int subtraction()
{
int numone, numtwo, answer;
numone=randomgen(200, 799);
numtwo=randomgen(800, 1);
printf("what is %d-%d ", numone, numtwo);
cin >> answer;
cout << "\n";
if(answer==numone-numtwo)
{
score++;
return 0;
}
else
{
wrongs++;
return 0;
}
}
int multiplication()
{
int numone, numtwo, answer;
numone=randomgen(900, 100);
numtwo=randomgen(750, 50);
printf("what is %d*%d ", numone, numtwo);
cin >> answer;
cout << "\n";
if(answer==numone+numtwo)
{
score+=2;
return 0;
}
else
{
wrongs++;
return 0;
}
}
int division()
{
int numone, numtwo, answer;
numtwo=randomgen(350, 50);
numone=numtwo*randomgen(900, 100);
printf("what is %d/%d ", numone, numtwo);
cin >> answer;
cout << "\n";
if(answer==numone/numtwo)
{
score+=2;
return 0;
}
else
{
wrongs++;
return 0;
}
}
int simplelinear()
{
int numone, slope, intercept, answer, correct;
slope=randomgen(50, 1);
intercept=randomgen(300, 500);
correct=randomgen(200, 1);
numone=slope*correct-intercept;
printf("what is x if %dx+%d=%d ", slope, intercept, numone);
cin >> answer;
cout << "\n";
if(answer==correct)
{
score+=2;
return 0;
}
else
{
wrongs++;
return 0;
}
}
I have a slight problem with simplelinear. It is not getting the correct answer. Could someone please tell me what I did wrong
EDIT: Norton only blocks in when I compile it in Microsoft visual C++. Dev C++ seems to not cause "viruses".