Solved

If statement accepting a through z only... need help!

Posted on 2007-12-06
8
309 Views
Last Modified: 2007-12-09
I need help with this code I need my if statement to accept a through z only and if a number or something else is entered it would fall into the else statement. When I entered a word if goes into the else I do not know what it is doing this.

Here is what I have for this file:
#include <iostream>
#include <string>
#include <fstream>
#include "game.h"
 
#define MaxWordSize 10
#define MaxWords 50
typedef char String[MaxWordSize];
String Words[MaxWords - 1];
int Count;
char Word[MaxWordSize];
int Size;
int Loop;
Game myGame;
 
using namespace std;
 
void Game::addWord()
{
    myGame.loadFile();
    ofstream Datfile("words.txt");
try
{
    cout << "\tEnter a new word, keep it below " << MaxWordSize << " characters: ";
    cin >> Word;
	if(Word == "ABCDEFGHIJKLMNOPQRSTUVWXYZ")
	{
    Size = strlen(Word);
 
    strcpy_s(Words[Count++],MaxWordSize,Word);
 
    for (int i = 0; i < Count; ++i) Datfile << Words[i] << endl;
	}
	else
	{
		throw 1;
	}
}
	catch(int e)
	{
		cout << "Error...\n";
	}
}
void Game::deleteWord()
{
	cout << "you have chosen to delete a word...\n";
}
 
void Game::loadFile()
{
	char C;
	ifstream Datfile;
 
	Count = 0;
 
	Datfile.open("words.txt");
 
	while((C=Datfile.peek()) !=EOF)
	{
		Datfile >> Words[Count++];
 
		if(Count > MaxWords - 1)
		{
			cout << "\nToo many words in the file, stopping with " << MaxWords << " words." << endl;
			Count = MaxWords;
			break;
		}
	}
 
	Count--;
 
	Datfile.close();
}

Open in new window

0
Comment
Question by:jschmuff
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 300 total points
ID: 20425122
>>      if(Word == "ABCDEFGHIJKLMNOPQRSTUVWXYZ")

this won't work at all, you can design your own function, so you can use  like:

      if(validateWord(Word))

Here is the validating funcion:


// this will require: #include <stdio.h>
 
bool validateWord(char *word)
{
       for (int i=0; word[i]; i++)
            if (!isletter(word[i])
                  return false;
       return true;
}

Open in new window

0
 

Author Comment

by:jschmuff
ID: 20425168
what does that have to do with taking the input only a through z?
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20425181
it validates if all characters in Word are A though Z and returns you a boolean value.
your code:
if(Word == "ABCDEFGHIJKLMNOPQRSTUVWXYZ")
doesn't work as you expected and always return false, so exception will never occur.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20425186
sorry, in this case, exception will always occur.
0
 

Author Comment

by:jschmuff
ID: 20425202
so that is something that would be done before the if statement since if(validateWord(Word)) is that correct.
so to actually do a - z I have to use a for loop? What is the isletter in this example you have given me? cause it is something that will need to be declared.
0
 

Author Comment

by:jschmuff
ID: 20425211
Is there another way this can be done like if(number is input) { throw 1; } else then the code to add and save word to file.. catch blah blah blah?
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20425236
>>so that is something that would be done before the if statement since if(validateWord(Word)) is that correct
validateWord is a separate function.

>>so to actually do a - z I have to use a for loop?
yes, you have to use a loop

>>What is the isletter in this example you have given me
isletter is a function declared in the stdio.h header, it test if a character is a letter or not. But you can use isalpha instead, it is more compatible.
If you don't want to use it, you can do this instead:


bool validateWord(char *word)
{
       char c; 
       for (int i=0; c = word[i]; i++)
            if (!(c>='A' && c<='Z') && !(c>='a' && c<='z'))
                  return false;
       return true;
}

Open in new window

0
 
LVL 40

Assisted Solution

by:evilrix
evilrix earned 200 total points
ID: 20434994
use std::string::find_first_not_of: -

http://www.cppreference.com/cppstring/find_first_not_of.html
char const * Word = "qwerty"; // Your word!
 
if(std::string::npos != std::string(Word).find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"))
{
	throw std::runtime_error("Invalid data");
}

Open in new window

0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question