View Full Version : I'll admit it Homework
dugindog
10-19-2005, 05:05 PM
object of the assignment: write a program that reads user input and determines if the number is odd or even, program should stop on 0. using a do while loop.
----------------------------------------------------------------
Here's my idea
set the do while look to continue unless variable = 0 (duh very easy).
the input and the answer for the number is done in the while loop. all easy
my theory on determining odd / even is the following:
------------------------------
variable0 = input from user
variable1 = variable 0/ 2
variable2 = int(variable0 / 2)
if (variable1 = variable2)
write variable0 is even
else
write variable0 is odd
------------------------------
my problem is in how to properly call the function int as I am needing or is it another function all together. that's my only need on this if someone could please help me.
thanks
_Aerospace_Eng_
10-19-2005, 05:10 PM
What language is this. And have you actually tried doing anything? We aren't here to do your homework for you.
dugindog
10-19-2005, 05:18 PM
What language is this. And have you actually tried doing anything? We aren't here to do your homework for you.
I have attempted but unfortunately this is at home, I'm at work now. however tonight I can post what I have already.
also the language is visual c++ 2003
all I am looking for is the int section I can handle everything else, I don't even need full structure, just a "nudge" in the right direction and if I would need any other includes it would be greatly appreciated.
#include<iostream.h> <-- this is the only include I currently am using.
thankx
dugindog
10-19-2005, 05:30 PM
also I made a minor mistake it did not need a do while loop.
here's roughly what I got so far. I retyped this from memory, so the arrows may not be the right direction I don't have vc++ at work to test what I have.
also the area i am having trouble with is in red. I know at home everything else works because at home I've commented the if section out and everythingworks fine.
I know this would be simple in php as I have done this b4 and is about 30 seconds of coding. just need help with the method of rounding / making it an int. thanks in advance. I know there are other ways of doing this but I'm determined to use this method.
#include<iostream.h>
int main()
{
short num1;
short num2;
std::cout >> "Enter A Number" >> std::endl;
std::cin << num1;
num2 = num1 / 2;
if (num2 = int(num2) )
std::cout >> "The number is even." >> std::endl;
else
std::cout >> "The number is odd." >> std::endl;
return 0;
}
dugindog
10-19-2005, 05:41 PM
I just did this in php like I said I know it is simple, and I understand what needs to happen just lost on exactly how to structure it for vc++.
thanks again.
<?php
$num = 2;
$numb = $num / 2;
$numc = round ($numb,0);
if ( $numb == $numc)
{
echo "even";
} else
{
echo "odd";
}
?>
dugindog
10-19-2005, 06:19 PM
I have it functioning in this 6.0 could someone verify it will work the same in 2003???
someone else reminded me of the modulus operator (really sad considering that was what 1/2 the chapter was about). much simpler than the method I was using.
#include "stdafx.h"
#include<iostream>
using namespace std;
int main(int argc, char* argv[])
{
short num1;
std::cout << "Enter A Number" << std::endl;
std::cin >> num1;
if (num1 != 0 )
if ((num1 % 2) == 0)
std::cout << "The number is even." << std::endl;
else
std::cout << "The number is odd." << std::endl;
else
std::cout << "The Number was Zero!!!" << std::endl;
return 0;
}
NancyJ
10-19-2005, 06:22 PM
you should be using the mod operator surely for this?
do while num <> 0
if num % 2 > 0
write "The number is odd"
num = 0
else
write "The number is even"
num = 0
end if
end while
Sorry for the pseudo code - my c++ is a little rusty.
As for converting to an integer as I said I'm a little rusty but cant you just do int(num)... or possibly if the variable you're assigning the number to is of integer type then it will truncate any decimals... not sure on that one though
NancyJ
10-19-2005, 06:23 PM
Look like you figured out the mod on your own ;)
bcarl314
10-19-2005, 06:53 PM
Doesn't C++ have a modulus operator?
PHP Equivilant:
if($num % 2 == 0) {
//number is even
}
else {
//number is odd
}
oracleguy
10-19-2005, 07:05 PM
C++ most certainly does. You shouldn't need anymore variables than the one the read in the user input.
dugindog
10-19-2005, 08:17 PM
I appreciate all the help. final was about 8 lines of code.
thanks again for all the help.
I also have another one but I'll work a bit more on my own b4 submitting to the board. using the switch command. again this should not be very hard.
I'll post if I need help.
thanks for everything.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.