5 Replies - 1493 Views - Last Post: 26 October 2006 - 05:19 AM Rate Topic: -----

#1 csstarter9  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 18-September 06

Strings

Posted 20 October 2006 - 06:07 AM

This is my program and what it is to do:

Quote

Write a program that asks the user to enter a single real (float) number (positive or negative) and outputs twice the number the user entered.
The program must validate the input of the number by first storing it into a character string and then verifying that it only contains digits, decimal point, and negative sign.
Limit the size of the array to 30 characters.
Verify that the maximum number of decimal points in the string is one (1).
Verify that the negative sign (if any) can only be at the first position of the string.
If any of the validation conditions fail, notify the user and request the number again until it is correct.
Once the string is validated, convert the string to float and output the number multiplied by two.
There is no requirement for this program to contain additional functions (other than main) or to request the user if he/she would want to enter a number again after validated.


This is what i have so far, am i off to a good start and what advise or help could you give me.

#include <iostream>


using namespace std;

int main()
{
	char s0;
	char s1[30];
	cout << "Enter a number: ";
	s0 = cin.get();
	cin.get(s1,30);
	cout << s0 << "\n";
	cout << s1 << "\n\n";

	char s2[30];
	char s3[30];
	char s4[30];
	cout << "Enter a number: ";
	cin.getline(s2,30,' ');
	cin.getline(s3,30,' ');
	cin.getline(s4,30,' ');
	cout << s2 << s3 << s4 << "\n\n";
	
	char s5[30];
	char s6[30];
	char s7[30];
	cout << "Enter a number: ";
	cin.get(s5,30,' ');
	cin.get(s6,30,' ');
	cin.get(s7,30,' ');
	cout << s5 << s6 << s7 << "\n\n";

	return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Strings

#2 rockstar_  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 32
  • View blog
  • Posts: 189
  • Joined: 16-October 06

Re: Strings

Posted 20 October 2006 - 02:54 PM

	char s2[30];
	char s3[30];
	char s4[30];
	cout << "Enter a number: ";
	cin.getline(s2,30,' ');
	cin.getline(s3,30,' ');
	cin.getline(s4,30,' ');
	cout << s2 << s3 << s4 << "\n\n";
	
	char s5[30];
	char s6[30];
	char s7[30];
	cout << "Enter a number: ";
	cin.get(s5,30,' ');
	cin.get(s6,30,' ');
	cin.get(s7,30,' ');
	cout << s5 << s6 << s7 << "\n\n";


Well, yes, you're on the right track. What is the above code for? You only need to grab one number as a string from the user. Then you're supposed to validate that string against the requirements, reporting errors if they occur and then printing out 2x the number put in. That's it. So you don't need that code. Validate only that one variable, and then multiply it by two and print out the result.
Was This Post Helpful? 0
  • +
  • -

#3 csstarter9  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 18-September 06

Re: Strings

Posted 23 October 2006 - 06:17 AM

View Postrockstar_, on 20 Oct, 2006 - 02:54 PM, said:

	char s2[30];
	char s3[30];
	char s4[30];
	cout << "Enter a number: ";
	cin.getline(s2,30,' ');
	cin.getline(s3,30,' ');
	cin.getline(s4,30,' ');
	cout << s2 << s3 << s4 << "\n\n";
	
	char s5[30];
	char s6[30];
	char s7[30];
	cout << "Enter a number: ";
	cin.get(s5,30,' ');
	cin.get(s6,30,' ');
	cin.get(s7,30,' ');
	cout << s5 << s6 << s7 << "\n\n";


Well, yes, you're on the right track. What is the above code for? You only need to grab one number as a string from the user. Then you're supposed to validate that string against the requirements, reporting errors if they occur and then printing out 2x the number put in. That's it. So you don't need that code. Validate only that one variable, and then multiply it by two and print out the result.



so it would look like this?
#include <iostream>


using namespace std;

int main()
{
	char s0;
	char s1[30];
	cout << "Enter a number: ";
	s0 = cin.get();
	cin.get(s1,30);
	cout << s0 << "\n";
	cout << s1 << "\n\n";


and then what?
Was This Post Helpful? 0
  • +
  • -

#4 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Strings

Posted 23 October 2006 - 09:26 AM

Now that you've taken the input, you need to check what was provided. You need to traverse the character array and ensure tha parameters have been met. Check and see if the first element is either a negative sign or a digit...if not, generate an error and reprompt. While traversing the array, keep a count of how many decimal places you encounter...if more than one, generate an error and reprompt. If it passes, convert and multiply by two. you may widh to chec out the following functions:

http://www.cprogramm...od/isdigit.html

http://www.cplusplus...lib/strtod.html

http://www.cplusplus...tdlib/atof.html

It should be noted that the use of the atof() function can be problematic, however, since it would return 0.0 for both a coversion of '0.0' and an error. since you're validating upfront, however, you'l; be fine.
Was This Post Helpful? 0
  • +
  • -

#5 csstarter9  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 18-September 06

Re: Strings

Posted 25 October 2006 - 08:34 AM

Ok i've worked on it some more and this is what i have but i keep getting an error message:
#include <iostream>

using namespace std;


bool dot(char *sNumb)
{
	static dot_count=0;

	if(*sNumb=='.')
		dot_count+=1;
	else 
		return false;
	return dot_count<=1;
}

bool checkvalid(char *sNumb)
{

	int n=strlen(sNumb);
	int i=0;
	if(*sNumb=='+' || *sNumb=='-')
	{
		if(n==1)
			return false;
		i=1;
		sNumb+=1;
	}
	else 
		i=0;
	
	for(i;i<n;i++,sNumb++)
		if (!(*sNumb>='0' && *sNumb<='9'))
		{
			if(n==1)
				return false;
			
			if(!dot(sNumb))
				return false;
		}
return true;
}

bool readnumer(float &fNumber)
{
	char *sNumb=new char[30];
	cout<<"Enter a number:";
	cin>>sNumb;
	if(checkvalid(sNumb)==true)  
	{
		fNumber=atof(sNumb);
		delete sNumb;
	}
	else
	{				
		delete sNumb;
		return false;
	}



	return true;
}
int main()
{
	float fNumber;

	while(!readnumer(fNumber))  
		cout<<"Invalid float number. Please try again!\n";
	cout<<"Twice your number is:"<<fNumber*2;
	return 0;
}


any help would be nice thank you
Was This Post Helpful? 0
  • +
  • -

#6 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Strings

Posted 26 October 2006 - 05:19 AM

What is the error message you are receiving?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1