Hello all. I am new to the board and hope to build great relationships with other forum members. I am working on a program for class that is supposed to show the GCD. I used a text editor to make it, but when i put it in Visual express 2008, it keeps showing error messages, when I thought I did it properly. Can anyone show me what I did wrong so I can make it work in visual? Thank you very much

``````/*******************************************************/
/*  File: name of your file with the source code       */
/*                                                     */
/* Created by: give your  name                         */
/* Date: give the date                                 */
/*                                                     */
/* Program to compute the GCD of two integers          */
/*                                                     */
/* Inputs: (keyboard)                                  */
/* 1. Two numbers - only positive numbers allowed       */
/*                                                     */
/* Output:                                             */
/* Print the GCD of given numbers on the screen        */
/*                                                     */
/* Algorithm: Euclid's Algorithm                       */
/*                                                     */
/*******************************************************/

#include "stdafx.h"
#include <iostream>

using namespace std ;

int main()
{

Declare x, y, temp, remainder as Integer.
int x;
int y;
int temp
int remainder = 1;

// read in the two integers

cout << endl ;
cout << "Enter the first number (positive integer) : " ;
cin >> x ;
cout << "Enter the second number (positive integer) : " ;
cin >> y ;

//echo inputs

cout << "Input numbers are: " << x << " , " << y << endl ;

Write a C++ if statement to determine if x < y.

if ((x == 0) || (y == 0))
{
cout << "ERROR:  Value of 0 has been entered."
return (-1);
}

if (x < y)
{ // exchange values of x and y
temp = x;
x = y;
y = temp;
}

/* At this point we will always have x >= y */

Initialize remainder.

while ( remainder != 0  )
{
Write the loop expression and loop body code in C++.
In C++, the expression (x % y) gives the remainder
after dividing x by y.

// value of q
temp = x / y;

// value of r
remainder = x % y;

// replace x with y
x = y;

// replace y with remainder
y = remainder;

}

// display the result
cout << endl ;
cout << "The GCD is: " << y << endl ;

return (0); // terminate with success
}
``````

Edited by happygeek: fixed formatting

Attachments
``````/*******************************************************/
/*  File: name of your file with the source code       */
/*                                                     */
/* Created by: give your  name                         */
/* Date: give the date                                 */
/*                                                     */
/* Program to compute the GCD of two integers          */
/*                                                     */
/* Inputs: (keyboard)                                  */
/* 1. Two numbers - only positive numbers allowed       */
/*                                                     */
/* Output:                                             */
/* Print the GCD of given numbers on the screen        */
/*                                                     */
/* Algorithm: Euclid's Algorithm                       */
/*                                                     */
/*******************************************************/

#include "stdafx.h"
#include <iostream>

using namespace std ;

int main()
{

Declare x, y, temp, remainder as Integer.
int x;
int	y;
int	temp
int	remainder = 1;

// read in the two integers

cout << endl ;
cout << "Enter the first number (positive integer) : " ;
cin >> x ;
cout << "Enter the second number (positive integer) : " ;
cin >> y ;

//echo inputs

cout << "Input numbers are: " << x << " , " << y << endl ;

Write a C++ if statement to determine if x < y.

if ((x == 0) || (y == 0))
{
cout << "ERROR:  Value of 0 has been entered."
return (-1);
}

if (x < y)
{ // exchange values of x and y
temp = x;
x = y;
y = temp;
}

/* At this point we will always have x >= y */

Initialize remainder.

while ( remainder != 0  )
{
Write the loop expression and loop body code in C++.
In C++, the expression (x % y) gives the remainder
after dividing x by y.

// value of q
temp = x / y;

// value of r
remainder = x % y;

// replace x with y
x = y;

// replace y with remainder
y = remainder;

}

// display the result
cout << endl ;
cout << "The GCD is: " << y << endl ;

return (0); // terminate with success
}``````
2
Contributors
1
4
Views
10 Years
Discussion Span
Last Post by mitrmkar

Please use code tags when you post code.

Mostly you have there comments not marked as comments, and also a couple of semicolons were missing ...
Use the VC++ editor to edit code, so you'll benefit from its syntax colouring, effectively meaning less errors.

``````#include "stdafx.h"
#include <iostream>

using namespace std ;

int main()
{

// Declare x, y, temp, remainder as Integer.
int x;
int y;
int temp ;
int remainder = 1;

// read in the two integers

cout << endl ;
cout << "Enter the first number (positive integer) : " ;
cin >> x ;
cout << "Enter the second number (positive integer) : " ;
cin >> y ;

//echo inputs

cout << "Input numbers are: " << x << " , " << y << endl ;

// Write a C++ if statement to determine if x < y.

if ((x == 0) || (y == 0))
{
cout << "ERROR: Value of 0 has been entered." ;
return (-1);
}

if (x < y)
{ // exchange values of x and y
temp = x;
x = y;
y = temp;
}

/* At this point we will always have x >= y */

// Initialize remainder.

while ( remainder != 0 )
{

/*
Write the loop expression and loop body code in C++.
In C++, the expression (x % y) gives the remainder
after dividing x by y.
*/

// value of q
temp = x / y;

// value of r
remainder = x % y;

// replace x with y
x = y;

// replace y with remainder
y = remainder;

}

// display the result
cout << endl ;
cout << "The GCD is: " << y << endl ;

return (0); // terminate with success
}``````
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.