Simple If statement problem
May 1, 2013 at 2:52pm UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
// Geometry Chapter 9 Project.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "iostream"
#include "stdio.h"
#include <cstdlib>
#include <conio.h>
#include <time.h>
#include <ctime>
using namespace std;
void QuestionOne ()
{
cout << "Question 1:" << endl << endl;
int q1x, q1y, q1vx, q1vy, q1ax, q1ay;
q1x = rand() % 10 + 1;
q1y = rand() % 10 + 1;
q1vx = rand() % 10 + 1;
q1vy = rand() % 10 + 1;
cout << "Point " << q1x << ", " << q1y << " is being translated with vector <" << q1vx << ", " << q1vy << ">." << endl << "What are the coordinated of the new image formed?" << endl;
cout << "x = " ;
cin >> q1ax;
cout << endl << "y = " ;
cin >> q1ay;
cout << endl;
if (q1ax == (q1x + q1vx) && q1ay == ( q1y + q1vy)) {
cout << "Correct! Next Question..." << endl << endl;
QuestionTwo ();
}
else {
cout << "Incorrect! Please try again" << endl << endl;
QuestionOne ();
}
}
void QuestionTwo ()
{
int q2x, q2y, q2ax, q2ay;
q2x = rand() % 10 + 1;
q2y = rand() % 10 + 1;
q2ax = q2y;
q2ay = q2x;
cout << "Point " << q2x << ", " << q2y << "is being rotated 90 degrees clockwise." << endl << "What are the coordinates after the rotation?" << endl << endl;
cout << "x = " ;
cin >> q2ax;
cout << endl << "y = " ;
cin >> q2ay;
cout << endl;
if (q2ax == q2y && q2ay == q2x) {
cout << "Correct!" << endl << endl;
}
else {
cout << "Incorrect! Try again..." << endl << endl;
QuestionTwo();
}
}
int main()
{
srand(time(0));
cout << "Geometry Chapter 9" << endl << endl;
QuestionOne ();
system ("pause" );
return 0;
}
I am not getting a "Correct!" or "Incorrect!" whether I get it right or wrong. What's the problem? Plus I am receiving build errors but I don't know from where.
Last edited on May 1, 2013 at 2:53pm UTC
May 1, 2013 at 3:02pm UTC
A) remove all unneccesary includes, leave only
1 2
#include <iostream>
#include <ctime>
B) Forward declare QuestionTwo():
1 2 3 4 5 6 7 8 9 10
void QuestionTwo(); //Forward declaration
void QuestionOne()
{
//...
}
void QuestionTwo() //Definition
{
//...
C) How do you know if you have some runtime error if your project doesn't compile?
May 1, 2013 at 3:10pm UTC
To answer C when I build without compiling it says 0 succeeded 1 failed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
// Geometry Chapter 9 Project.cpp : Defines the entry point for the console application.
//
#include "iostream"
#include <ctime>
using namespace std;
void QuestionTwo();
void QuestionOne ()
{
cout << "Question 1:" << endl << endl;
int q1x, q1y, q1vx, q1vy, q1ax, q1ay;
q1x = rand() % 10 + 1;
q1y = rand() % 10 + 1;
q1vx = rand() % 10 + 1;
q1vy = rand() % 10 + 1;
cout << "Point " << q1x << ", " << q1y << " is being translated with vector <" << q1vx << ", " << q1vy << ">." << endl << "What are the coordinated of the new image formed?" << endl;
cout << "x = " ;
cin >> q1ax;
cout << endl << "y = " ;
cin >> q1ay;
cout << endl;
if (q1ax == (q1x + q1vx) && q1ay == ( q1y + q1vy)) {
cout << "Correct! Next Question..." << endl << endl;
QuestionTwo ();
}
else {
cout << "Incorrect! Please try again" << endl << endl;
QuestionOne ();
}
}
void QuestionTwo ()
{
int q2x, q2y, q2ax, q2ay;
q2x = rand() % 10 + 1;
q2y = rand() % 10 + 1;
q2ax = q2y;
q2ay = q2x;
cout << "Point " << q2x << ", " << q2y << "is being rotated 90 degrees clockwise." << endl << "What are the coordinates after the rotation?" << endl << endl;
cout << "x = " ;
cin >> q2ax;
cout << endl << "y = " ;
cin >> q2ay;
cout << endl;
if (q2ax == q2y && q2ay == q2x) {
cout << "Correct!" << endl << endl;
}
else {
cout << "Incorrect! Try again..." << endl << endl;
QuestionTwo();
}
}
int main()
{
srand(time(0));
cout << "Geometry Chapter 9" << endl << endl;
QuestionOne ();
system ("pause" );
return 0;
}
Is this what you mean by forward declaring? Because this didn't work but I might have done it wrong. Thanks for the help
May 1, 2013 at 3:19pm UTC
It is <iostream>
and your code works fine for me (aside from wrong answer calculation in QuestionTwo). Might be something in your project settings. Try to create a new project and copy code there.
Last edited on May 1, 2013 at 3:19pm UTC
May 1, 2013 at 3:27pm UTC
Hmm..still getting "1 failed" even with a new project I'm using Visual Express C++ 2010. What settings should I have? And I thought that when you rotate a point with positive coordinates the x and y values switch?
May 1, 2013 at 3:37pm UTC
IT WORKED! My problem was that I checked the "Use precompiled header". Thank you so much MiiNiPaa!
Topic archived. No new replies allowed.