By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,501 Members | 1,704 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,501 IT Pros & Developers. It's quick & easy.

Not returning correct information

P: 6
Hello, I am very new to the C++ world (2 months)

I have an assignment where we need to write a program that asks the user for his first name in lowercase, validate it, and return it. Then ask for their last name in lowercase, validate it, and return it. Then return the full name and show how many characters it consist of.

My validation works fine but it's not returning the name.

ie...Please enter your first name in lowercase: mike
Your first name in lowercase is

Here is what I have thus far:
(Also the input of each string must be done through a function and the function must return a Boolean parameter indicating the validity of the input)


//Assignment07, Pointers, Functions, and Input Validation
//Michael
//November, 12, 2008

#include <iostream>
#include <cstdlib>

using namespace std;

bool lowerCase(char *);

void main()
{
char first, last;
bool validLetters;

do
{
cout << "Enter your first name in lower case letters: ";
validLetters = lowerCase(&first);
if (!validLetters) cout << "Invalid entry. Try again...\n";
} while (!validLetters);

cout << "\n\nYour first name in lower case is: " << first << endl;

do
{
cout << "Enter your last name in lower case letters: ";
validLetters = lowerCase(&last);
if (!validLetters) cout << "Invalid entry. Try again...\n";
} while (!validLetters);
cout << "\n\nYour last name in lower case is: " << last << endl;

}

bool lowerCase(char *chars)
{
int i,len;
char buf[100];
bool validLetters = true;

cin.getline(buf,100);
len = strlen(buf);
i = 0;
while (validLetters && i < len)
{
if ((buf[i] < 'a') || (buf[i] > 'z'))
validLetters = false;
i++;
}

if (validLetters)
{
(*chars) = atoi(buf);
if (((*chars) < 0) || ((*chars) > 100))
validLetters = false;
}

return validLetters;
}


Thank you for your help. And I thought Spanish was rough..lol
Nov 11 '08 #1
Share this Question
Share on Google+
12 Replies


Expert 10K+
P: 11,448
A single char can never store an entire name; use a char[] (an array of chars)
or a C++ string instead.

kind regards,

Jos
Nov 11 '08 #2

boxfish
Expert 100+
P: 469
Your function is not using the argument you pass to it. Seeing as that argument consists of a pointer to only one letter, it's not very useful anyway. Maybe what you meant to do was create an array of chars like buf in main, and then pass it to the function and have the function read into it, so that what the user entered can be accessed from main after the function is done.
Hope this makes sense. Good luck.
Nov 11 '08 #3

Ganon11
Expert 2.5K+
P: 3,652
It might be a good idea to separate your input function from your validation function. That is, instead of one function that gets the input and validates it, try two functions: one to get the string. It doesn't care what gets put into the string, it just gets the string. Your second function takes a string and returns true if it meets your criteria (in this case, all lowercase 'letter' characters), false otherwise. Then your while loop will look something like:

Expand|Select|Wrap|Line Numbers
  1. do {
  2.    cout << "Prompt for string" << endl;
  3.    getInput(firstName);
  4. } while (!valid(firstName));
Or don't even use a separate function for getting the input. If you use an std::string instead of messy char*s, it's as easy as using cin >> firstName.

My overwhelming suggestion would be to use std::strings instead of char[]s and char*s - it will save you a LOT of trouble down the line.
Nov 11 '08 #4

P: 6
I understand why I have to use an array. I tried changing my char variable to arrays and now I get two errors:
Error 1 error C2664: 'lowerCase' : cannot convert parameter 1 from 'char (*)[100]' to 'char *'

here is what I have:

#include <iostream>
#include <cstdlib>

using namespace std;

bool lowerCase(char *);

void main()
{
char first[100];
char last[100];
bool validLetters;

do
{
cout << "Enter your first name in lower case letters: ";
validLetters = lowerCase(&first);
if (!validLetters) cout << "Invalid entry. Try again...\n";
} while (!validLetters);

cout << "\n\nYour first name in lower case is: " << first << endl;

do
{
cout << "Enter your last name in lower case letters: ";
validLetters = lowerCase(&last);
if (!validLetters) cout << "Invalid entry. Try again...\n";
} while (!validLetters);
cout << "\n\nYour last name in lower case is: " << last << endl;

}

bool lowerCase(char *chars)
{
int i,len;
char buf[100];
bool validLetters = true;

cin.getline(buf,100);
len = strlen(buf);
i = 0;
while (validLetters && i < len)
{
if ((buf[i] < 'a') || (buf[i] > 'z'))
validLetters = false;
i++;
}

if (validLetters)
{
(*chars) = atoi(buf);
if (((*chars) < 0) || ((*chars) > 100))
validLetters = false;
}

return validLetters;
}
Nov 12 '08 #5

P: 6
Is it because my Function is only set for a single variable char?
Nov 12 '08 #6

boxfish
Expert 100+
P: 469
first and last are already char*s. You don't have to take their addresses with &, just pass them to the function. That should clear up your errors. Your function is still not reading into the char array that you pass to it. Get rid of buf and read into chars.
Hope this helps.
Edit:
By the way, it would be helpful if you used code tags around your code. Put [CODE] before the code and [/CODE] after it, so it shows up in a code box and the indentation isn't wrecked. Thanks.
Nov 12 '08 #7

P: 6
Thank you boxfish....Can I borrow your brain for the rest of the semester. I really would like to learn this. I have bought two additional books, besides what the class requires....something just isn't clicking.

But I did what you said and indeed, it cleared up those two errors, but something still isn't right...

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <cstdlib>
  3.  
  4. using namespace std;
  5.  
  6. bool lowerCase(char *);
  7.  
  8. void main()
  9. {
  10. char first[100];
  11. char last[100];
  12. bool validLetters;
  13.  
  14. do
  15. {
  16. cout << "Enter your first name in lower case letters: ";
  17. validLetters = lowerCase(first);
  18. if (!validLetters) cout << "Invalid entry. Try again...\n";
  19. } while (!validLetters);
  20.  
  21. cout << "\n\nYour first name in lower case is: " << first << endl;
  22.  
  23. do
  24. {
  25. cout << "Enter your last name in lower case letters: ";
  26. validLetters = lowerCase(last);
  27. if (!validLetters) cout << "Invalid entry. Try again...\n";
  28. } while (!validLetters);
  29. cout << "\n\nYour last name in lower case is: " << last << endl;
  30.  
  31. }
  32.  
  33. bool lowerCase(char *chars)
  34. {
  35. int i,len;
  36. char chars[100];
  37. bool validLetters = true;
  38.  
  39. cin.getline(chars,100);
  40. len = strlen(chars);
  41. i = 0;
  42. while (validLetters && i < len)
  43. {
  44. if ((chars[i] < 'a') || (chars[i] > 'z'))
  45. validLetters = false;
  46. i++;
  47. }
  48.  
  49. if (validLetters)
  50. {
  51. (*chars) = atoi(chars);
  52. if (((*chars) < 0) || ((*chars) > 100))
  53. validLetters = false;
  54. }
  55.  
  56. return validLetters;
  57. }
  58.  

I am now getting a Error 1 error C2082: redefinition of formal parameter 'chars' On line 36

Thanks for all of your help.
Nov 12 '08 #8

boxfish
Expert 100+
P: 469
I am now getting a Error 1 error C2082: redefinition of formal parameter 'chars' On line 36
It says what it means and it means what it says; chars is already declared as the parameter to the function and there's no need to declare it again.
Can I borrow your brain for the rest of the semester.
Sorry, I need it for Calc 1. :-)
Nov 12 '08 #9

P: 6
I deleted that line of code and now I get now errors but......it still returns nothing.

ie...Please enter your first name in lower case: mike
Your first name in lower case is


Ahhhh Calc1. I had to take a trig/calc class that was combined together. Made for many a fun night.

Luckily I am finished with Math for the remainder of my degree.

I really do appreciate all the help.
Nov 12 '08 #10

Banfa
Expert Mod 5K+
P: 8,916
On line 51, I do not know what you think the function atoi does (OK I could guess) I seriously suggest that you look it up in your text book or on the internet.

You can not use chars to return both the string and the length of the string.
Nov 12 '08 #11

P: 6
Thanks Banfa....

One last question and I will leave you alone, I hope.

How would you return the total number of characters in the first and last name entered.

I have played around with it, but it keeps returning the number of the entire array, not sure how to tell it to stop at the null terminator.

Thanks
Nov 12 '08 #12

Ganon11
Expert 2.5K+
P: 3,652
If you're using char*s or char[]s, check the strlen function.
Nov 12 '08 #13

Post your reply

Sign in to post your reply or Sign up for a free account.