Private Messages Options Search Blogs Images Chat Cam Portals Calendar FAQ's Join  
Asylum Forums : Powered by vBulletin version 2.2.8 Asylum Forums > WIT - Whore Institute of Technology > I need some help with some C++ code
  Last Thread   Next Thread
Author
Thread [new thread]    [post reply]
SocialParasite
100% pure failtanium.

Registered: Jul 2000
Location: Beatrice, Nebraska
Posts: 18489

Post I need some help with some C++ code

I've written some code for an asignment, but there are some bugs in it, and I can't figure it out. Maybe someone here can:

quote:

/* Computer diagnosis program */


#include <iostream.h>
#include <conio.h>
int main()

{
cout << "--Computer Diagnostic Program--" <<endl;
cout << " " <<endl;
cout << "Does the computer beep at startup? (y/n?)" <<endl;
char Beep;
Beep = getche();
cout << "Does the hard drive spin? (y/n?)" <<endl;
char Spin;
Spin = getche();
if (Beep && Spin == 'y' | | Beep && Spin == 'Y')
cout << "Contact tech support.";
else if (Beep == 'y' && Spin == 'n' | | Beep == 'Y' && Spin == 'N')
cout << "Check drive contacts.";
else if (Beep && Spin == 'n' | | Beep && Spin == 'N')
cout << "Bring the computer to the repair center.";
else if (Beep == 'n' && Spin == 'y' | | Beep == 'N' && Spin = 'Y')
cout << "Check the speaker contacts." <<endl;


return(0);

}



What's wrong?

------------------
Have a good day, and if you feel like subjecting your genitals to high doses of x-ray radiation I'm sure the world will kindly thank you.

Report this post to a moderator | IP: Logged

Old Post 02-23-2001 08:32 PM
SocialParasite is offline Click Here to See the Profile for SocialParasite Click here to Send SocialParasite a Private Message Visit SocialParasite's homepage! Find more posts by SocialParasite Add SocialParasite to your buddy list [P] Edit/Delete Message Reply w/Quote
macker
Holy Me-el

Registered: Nov 2000
Location: UK
Posts: 4735

Post

Please...indent...code...before...I...go...nuts...



------------------------
Individualists unite!

Report this post to a moderator | IP: Logged

Old Post 02-24-2001 06:18 AM
macker is offline Click Here to See the Profile for macker Click here to Send macker a Private Message Visit macker's homepage! Find more posts by macker Add macker to your buddy list [P] Edit/Delete Message Reply w/Quote
Dingle
Prison Rapemaster

Registered: Jul 2000
Location: Minneapolis, MN
Posts: 10178

Post

first big prob is in this line:

code:
if (Beep && Spin == 'y' | | Beep && Spin == 'Y')


that just doesn't work, what you're saying is if Beep isnt null
and Spin equals y...
each comparison needs to be seperate...
code:
if(Beep == 'y' && Spin == 'y' | | Beep == 'Y' && Spin == 'Y')

if Beep equals y and spin equals y OR Beep equals Y and Spin equals Y
makes more sense right?
but what if someone enters a lowercase y for Beep but an uppercase Y for Spin?
code:
if(Beep == 'y' | | Beep == 'Y" && Spin == 'y' | | Spin == 'Y")

if Beep equals y OR Beep equals Y AND Spin equals y or Spin equals Y
now it is case insensitive


another problem is in this line:
code:
else if (Beep == 'n' && Spin == 'y' | | Beep == 'N' && Spin = 'Y')

prolly just a typo but = is an assignment operator, needs to be == comparison operator


also, if you want me to get picky, you should declare all your variables at the beginning of
the function. you dont NEED to but it is considered good programming practice.

also testing cases is easier if you use booleans, and if you want to impress your instructor only allow valid input.

code:
#include <iostream.h> #include <conio.h> int main() { char buff = NULL; bool beep, spin, ok; beep = spin = ok = false; cout << "--Computer Diagnostic Program--" << endl; while(!ok) { ok = true; cout << "Does the computer beep at startup? (y/n?)" << flush; buff = getche(); if(buff == 'y' | | buff == 'Y') beep = true; else if(buff == 'n' | | buff == 'N') beep = false; else { cout << endl << "Invalid Response. "; ok = false; } } cout << endl << endl; ok = false; while(!ok) { ok = true; cout << "Does the hard drive spin? (y/n?)" << flush; buff = getche(); if(buff == 'y' | | buff == 'Y') spin = true; else if(buff == 'n' | | buff == 'N') spin = false; else { cout << endl << "Invalid Response. "; ok = false; } } cout << endl << endl; if(beep) { if(spin) cout << "Contact tech support." << endl; else cout << "Check drive contacts." << endl; } else { if(spin) cout << "Check the speaker contacts." << endl; else cout << "Bring the computer to the repair center." << endl; } cout << endl << endl; return(0); }


[This message has been edited by Dingle (edited 02-24-2001).]

Report this post to a moderator | IP: Logged

Old Post 02-24-2001 07:06 AM
Dingle is offline Click Here to See the Profile for Dingle Click here to Send Dingle a Private Message Find more posts by Dingle Add Dingle to your buddy list [P] Edit/Delete Message Reply w/Quote
macker
Holy Me-el

Registered: Nov 2000
Location: UK
Posts: 4735

Post

*sniffs and gets teary*

Thanks Dingle. That code, the indenting, was just beautiful...*sobs*

------------------------
Individualists unite!

Report this post to a moderator | IP: Logged

Old Post 02-24-2001 01:21 PM
macker is offline Click Here to See the Profile for macker Click here to Send macker a Private Message Visit macker's homepage! Find more posts by macker Add macker to your buddy list [P] Edit/Delete Message Reply w/Quote
SocialParasite
100% pure failtanium.

Registered: Jul 2000
Location: Beatrice, Nebraska
Posts: 18489

Post

Sorry about the indentation, or rather, the lack of it. I copied the code to Notepad and saved it to disk, and Notepad didn't keep the indentation.

I didn't state the variables at the beginning because I was writing the code as I thought it out.

Thanks Dingle, I'll give that code a try.

------------------------
Have a good day, and if you feel like subjecting your genitals to high doses of x-ray radiation I'm sure the world will kindly thank you.

Report this post to a moderator | IP: Logged

Old Post 02-24-2001 03:28 PM
SocialParasite is offline Click Here to See the Profile for SocialParasite Click here to Send SocialParasite a Private Message Visit SocialParasite's homepage! Find more posts by SocialParasite Add SocialParasite to your buddy list [P] Edit/Delete Message Reply w/Quote
All times are GMT. The time now is 06:22 PM. Post New Thread    Post A Reply
  Last Thread   Next Thread
Show Printable Version | Email this Page | Subscribe to this Thread

Forum Jump:
 

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is ON
 

< Contact Us - The Asylum >

Powered by: vBulletin Version 3.0.6
Copyright ©2000 - 2002, Jelsoft Enterprises Limited.
Copyright © 2000- Imaginet Inc.
[Legal Notice] | [Privacy Policy] | [Site Index]