Democratic Underground Latest Greatest Lobby Journals Search Options Help Login
Google

anyone know C++? need help with code Intro to Comp Sci Class

Printer-friendly format Printer-friendly format
Printer-friendly format Email this thread to a friend
Printer-friendly format Bookmark this thread
This topic is archived.
Home » Discuss » The DU Lounge Donate to DU
 
pstokely Donating Member (1000+ posts) Send PM | Profile | Ignore Tue Mar-09-04 02:29 AM
Original message
anyone know C++? need help with code Intro to Comp Sci Class
my textbook is C++ Programming today, author Barbara Johnston publisher Prentice Hall

this is the problem out of the book

"Write a complete C++ program that asks the user to input a character. Using the ASCII Character set as a guide, state whether the user's character is a digit (0 to 9), a letter (a to z or A to Z), or a symbol.

here is my code, my compiler is Microsoft Visual C++ 6.0, can't figure out the part about the letter and symbol

#include "stdafx.h"
#include <iostream>
#include <iomanip> //for setw()
using namespace std;

int main ()
{
char enter;
char runAgain = 'y';
int number;
char letter 'A'



const int lowestValidNumber = 1;
const int highesValidNumber = 9;

while (runAgain != 'n' && runAgain != 'N')
{
if (runAgain == 'y' || runAgain == 'Y')
{
cout << "\n Enter a letter, number, or symbol: ";
cin >> number;

if (number <= 9)

cout <<"\n '"<<number<<"' is a number" <<endl;



}

cout <<"\n Do another ? y = yes, n = no ";
cin >> runAgain;
cin.get(enter);
}
cout <<"\n Good Bye! Press ENTER to exit....";
cin.get(enter);
return 0;
}

Printer Friendly | Permalink |  | Top
kar_the_terrible Donating Member (12 posts) Send PM | Profile | Ignore Tue Mar-09-04 02:38 AM
Response to Original message
1. slight error in your variables
YOU WROTE:
 int number;
char letter 'A'



const int lowestValidNumber = 1;
const int highesValidNumber = 9;

while (runAgain != 'n' && runAgain != 'N')
{
if (runAgain == 'y' || runAgain == 'Y')
{
cout << "\n Enter a letter, number, or symbol:
";
cin >> number;

if (number <= 9)

cout <<"\n '"<<number<<"' is
a number" <<endl;

OK...
Here's the problem... we need to get a character from the
number.. so you need to first of all:

char c;
cin >> c;

Now, if the value in c is between '0' and '9' its obviously a
number, else if between 'a' to 'z' its an alphabet. We also
can use another trick.. in the ascii code 0-9 are continous,
and a-z are continuous. So we really dont need to knwo the
aSCII COdes per se... just do this

if ('0' <= c && '9' >= c) // we have a number
else if('a' <= c && 'z' >= c) // we have a
letter
Printer Friendly | Permalink |  | Top
 
CanuckAmok Donating Member (1000+ posts) Send PM | Profile | Ignore Tue Mar-09-04 02:39 AM
Response to Original message
2. Holy cats, I got a headache just opening this post!
I feel like Ralph Phillips, with the numbers trying to kill me.
Printer Friendly | Permalink |  | Top
 
Elad ADMIN Donating Member (1000+ posts) Send PM | Profile | Ignore Tue Mar-09-04 02:40 AM
Response to Original message
3. Been awhile since I did C++, but...
First of all it looks like your lowestValidNumber and highestValidNumber are not only incorrect, but unused. You define them but don't use them. lowestValidNumber should also be 0, not 1.

Finally, I think what the problem is getting at is that in the ASCII charset, all characters are represented by an integer. If I'm remembering my C correctly you can differentiate between a number, alphabet character, or symbol by what it's integer value is in the ASCII charset, and I think that's what the problem is trying to get at.
Printer Friendly | Permalink |  | Top
 
Syrinx Donating Member (1000+ posts) Send PM | Profile | Ignore Tue Mar-09-04 02:41 AM
Response to Original message
4. it depends on the ascii character set
pseudocode:

if x < 48:
xtype = symbol
elif x < 58:
xtype = numeral
elif x < 65:

elif x < 91:
xtype = letter
elif x < 97:


I can't enter my example fully, because DU is acting weird and won't let me.
Printer Friendly | Permalink |  | Top
 
burrowowl Donating Member (1000+ posts) Send PM | Profile | Ignore Tue Mar-09-04 02:43 AM
Response to Original message
5. I hate to say this, but
you should spend a little less time on DU and more time cracking your books.
Printer Friendly | Permalink |  | Top
 
Squeegee Donating Member (577 posts) Send PM | Profile | Ignore Tue Mar-09-04 03:53 AM
Response to Original message
6. You need to ...
... put a couple of goto statement in there somewhere. You'll be guaranteed an A for sure! ;)
Printer Friendly | Permalink |  | Top
 
Vladimir Donating Member (1000+ posts) Send PM | Profile | Ignore Tue Mar-09-04 07:22 AM
Response to Reply #6
7. Nooooooo
NOT THE GOTO! Anything but the evil spaghetti code monster... brain melts away... :)

V
Printer Friendly | Permalink |  | Top
 
DU AdBot (1000+ posts) Click to send private message to this author Click to view 
this author's profile Click to add 
this author to your buddy list Click to add 
this author to your Ignore list Fri Dec 27th 2024, 10:37 AM
Response to Original message
Advertisements [?]
 Top

Home » Discuss » The DU Lounge Donate to DU

Powered by DCForum+ Version 1.1 Copyright 1997-2002 DCScripts.com
Software has been extensively modified by the DU administrators


Important Notices: By participating on this discussion board, visitors agree to abide by the rules outlined on our Rules page. Messages posted on the Democratic Underground Discussion Forums are the opinions of the individuals who post them, and do not necessarily represent the opinions of Democratic Underground, LLC.

Home  |  Discussion Forums  |  Journals |  Store  |  Donate

About DU  |  Contact Us  |  Privacy Policy

Got a message for Democratic Underground? Click here to send us a message.

© 2001 - 2011 Democratic Underground, LLC