Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple string vaildation problem. 1

Status
Not open for further replies.

webmastadj84

IS-IT--Management
Aug 23, 2006
86
US
I need some help. I am trying to vaildate a string. The user needs to insert a number no bigger than 30 characters long and needs to be number. So no more than one "." and one "-". Also, I can't have any alpha charaters (i.e. a,b,c,d,e,f....).

This is what I have so far:
===============================================
#include <iostream>
#include <string>
using namespace std;

float DoubleANumber(float Num) {
return (Num*2);
}

int main() {
char ANumber[30];
bool InputValid = true;

cout << "Enter a number: ";
cin.getline(ANumber,30);

int LoopCount = 0;
int Decimals = 0;
int Negitave = 0;
int CharSize = strlen(ANumber);

while (CharSize > LoopCount) {
if (ANumber[LoopCount] = ".") {
Decimals = Decimals + 1;
}

if (Decimals > 1) {
InputValid = false;
}

LoopCount = LoopCount + 1;
}

if (InputValid = true) {
cout << "IT IS VALID";
} else {
cout << "IT IS NOT VALID";
}

cin >> ANumber;
return(0);
}
=========================================================

The problem is with line:

if (ANumber[LoopCount] = ".") {

When I try to complie the code (run debug), I get an output error of:
'=': cannot convert from 'const char [2]' to 'char'
There is no context in which this coversion is possible.

Please help!
 
= is for assignment. Use == for comparison.

You should also add an else clause and use isdigit() to see if the character is a number.
Also, add a check to see if the first character is a "-".
 
Oh, and one more problem... When comparing a single character, you need to put the character in single quotes, not double quotes. Ex.
Code:
if ( ANumber[LoopCount] == '.' )
 
Ok, thank you, I will try that out and let you know.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top