Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 08-31-2005, 12:21 PM   #1 (permalink)
Salchester
Salchester
 
Salchester's Avatar
 
Join Date: Jul 2005
Location: In a house
Posts: 230
Salchester is an unknown quantity at this point
If Statements

I am currently working on a program that will write two pieces of information to file, via a variable. The two pieces of data stored are the following:
  • Name
  • House Colour
The persons name is stored in a variable array called ‘name’, and the house is stored in a character variable called ‘housecolour’.

I need help on the if statement for the housecolour, this being to only allow the user to enter a character ‘G’ or ‘Y’. Any other character will lead to an Error message being displayed.

Only with the code I have written, the ‘Y’ get accepted but ‘G’ doesn’t why is this?

.::Code Enclosed below::.

Code:
#include <iostream.h>
#include <fstream.h>
#include <iomanip.h>
#include <conio.h>
#include <ctype.h>
#include <string.h>
#include <dos.h>

int main (void)
{
  char name[20];
  char housecolour;
  int ln;
  char choice;

  ofstream data (".\\Students.txt");

  do
  {
  gotoxy(52,14);
  cout << (char)218 << setw(9) << setfill((char)196) << (char)191 << endl;
  gotoxy(51,15);
  cout << (char)218 << (char)217 << setw(9) << setfill(' ') << (char)192 << setw(13) << setfill((char)196) << (char)191 << endl;
  ln = 16;
  do
  {
    gotoxy(51,ln);
    cout << (char)179 << setw(23) << setfill(' ') << (char)179 << endl;
    ln++;
  } while (ln != 19);
  gotoxy(51,19);
  cout << (char)192 << setw(23) << setfill((char)196) << (char)217 << endl;
  gotoxy(54,15);
  cout << "STATUS";
  gotoxy(53,17);
  cout << "Saved Successfully";
  gotoxy(9,11);
  cout << "Name: ";
  gotoxy(9,12);
  cout << "House Colour: ";
  gotoxy(15,11);
  cin >> name;
  do
  {
    gotoxy(23,12);
    cin >> housecolour;
    housecolour = toupper (housecolour);
    if (housecolour != 'Y' && 'G')
    {
      gotoxy(53,17);
      textcolor(LIGHTRED);
      cprintf ("Error             ");
    }
  } while (housecolour != 'Y' && 'G');
  gotoxy(53,17);
  textcolor(YELLOW);
  cprintf ("Awaiting Input");
  data << name << ' ';
  data << housecolour << endl;
  gotoxy(53,17);
  textcolor(GREEN);
  cprintf ("Saved Successfully");
  gotoxy(31,17);
  } while (choice != 'N');
  data.close();
}
__________________
Many Thanks, in advance!

Salchester.
The Future Is Here - Are You Ready?
Salchester is offline   Reply With Quote
Old 08-31-2005, 01:21 PM   #2 (permalink)
toe_cutter
Code Monkey
 
Join Date: Aug 2002
Location: Boston, MA
Posts: 79
toe_cutter is on a distinguished road
Send a message via ICQ to toe_cutter Send a message via AIM to toe_cutter Send a message via Yahoo to toe_cutter
This

if (housecolour != 'Y' && 'G')

Should probably be

if ( (housecolour != 'Y') && (housecolour != 'G') )

the same goes for your do/while statement

Toe
__________________
toe_cutter is offline   Reply With Quote
Old 08-31-2005, 02:25 PM   #3 (permalink)
Salchester
Salchester
 
Salchester's Avatar
 
Join Date: Jul 2005
Location: In a house
Posts: 230
Salchester is an unknown quantity at this point
Many Thanks. It works great, now why didn't i think of that?

Once again cheers!
__________________
Many Thanks, in advance!

Salchester.
The Future Is Here - Are You Ready?
Salchester is offline   Reply With Quote
Old 08-31-2005, 06:53 PM   #4 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Salchester, you are mixing C with C++. Try to use one or the other. Not both if possible. Right now it's possible.
__________________
Valmont is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help on MySQL Statements infinite_root Everything SQL ( MySQL, MSSQL, DB2, Postgre, Oracle, etc...) 5 04-21-2005 06:37 PM
If....then statements patelk MS Technologies ( ASP, VB, C#, .NET ) 0 12-21-2004 01:48 PM
Brain Melting: Trouble With If Statements..... UnusualGameBoy Java 3 10-27-2004 03:10 PM
If statements abc123 Java 8 09-19-2002 02:17 PM


All times are GMT -8. The time now is 04:10 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting