没有返回正确的信息 [英] Not returning correct information

查看:67
本文介绍了没有返回正确的信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我是C ++世界的新手(2个月)


我有一项任务,我们需要编写一个程序,要求用户以小写字母命名,验证它,并返回它。然后以小写形式询问他们的姓氏,验证它并返回它。然后返回全名并显示它包含的字符数。


我的验证工作正常,但它没有返回名称。


ie ...请输入您的小写名字:mike

您的小写字母是


这是我到目前为止的名字:

(每个字符串的输入也必须通过函数完成,函数必须返回一个布尔参数,指示输入的有效性)



// Assignment07,指针,函数和输入验证

// Michael

// 2008年11月12日


#include< iostream>

#include< cstdlib>


使用命名空间std;


bool lowerCase (char *);


void main()

{

char first,last;

bool validLetters;


do

{

cout<< 以小写字母输入您的名字:" ;;

validLetters = lowerCase(& first);

if(!validLetters)cout<< 无效的条目。再试一次...... \ n" ;;

} while(!validLetters);


cout<< " \ n \ n你小写的小写名字是:" <<第一个<<结束;


do

{

cout<< 以小写字母输入您的姓氏:" ;;

validLetters = lowerCase(& last);

if(!validLetters)cout<< 无效的条目。再试一次...... \ n" ;;

} while(!validLetters);

cout<< \ n \ n你小写的姓氏是: <<最后<< 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]<'''')||(buf [i]>''z'')) />
validLetters = false;

i ++;

}


if(validLetters)

{

(* chars)= atoi(buf);

if(((* chars)< 0)||((* chars)> 100))

validLetters = false;

}


返回validLetters;

}



感谢您的帮助。而且我认为西班牙语很粗糙.lol

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

推荐答案

单个字符不能存储整个名称;使用char [](字符数组)

或C ++字符串代替。


亲切的问候,


Jos
A single char can never store an entire name; use a char[] (an array of chars)
or a C++ string instead.

kind regards,

Jos


你的函数没有使用你传递给它的参数。看到该参数由一个只指向一个字母的指针组成,它无论如何都不是很有用。也许你打算做的是在main中创建一个像buf一样的字符数组,然后将它传递给函数并将函数读入其中,以便在函数完成后可以从main访问用户输入的内容。 br />
希望这是有道理的。祝你好运。
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.


将输入函数与验证函数分开可能是个好主意。也就是说,不是一个获取输入并验证它的函数,而是尝试两个函数:一个用于获取字符串。它并不关心什么被放入字符串,它只是得到字符串。你的第二个函数接受一个字符串,如果它符合你的标准(在这种情况下,所有小写的''字母''字符),则返回true,否则返回false。然后你的while循环看起来像:

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:

展开 | 选择 | Wrap | 行号


这篇关于没有返回正确的信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆