繁体   English   中英

允许用户输入名字和姓氏; 向用户显示全名(第一个+最后一个)

[英]Allow user to input first and last name; display full name back to user (first + last)

我知道这很简单,但我无法弄清楚我错过了什么或做错了什么。 我认为这可能与 char 变量有关。 这是我到目前为止:

#include<stdio.h> 
#include "stdafx.h"
#include<iostream>

using namespace std;

int main() 
{ 
char fname[20],lname[20];

cout<<"Please enter your First Name:";
cin>>"fname";
cout<<"Please enter your Last Name:";
cin>>"lname";

cout<<"Your full name is:"<<fname<<lname<<endl;

int a,b = 0;

for(a=0;a<=50;a++) 
{ 
if(a%3!=0&&a%4!=0&&a%5!=0) 
{ 
printf(" %d",a);
b++;
} 
} 
printf("\nNos of counts%d",b); 

} 
cin>>"fname";

您正在尝试提取到字符串文字"fname" 看来您打算提取到变量fname

cin>>fname;

自从约瑟夫回答了这个问题后,我只想提出一些建议。 在名字和姓氏之间添加一个空格,这样它就不会将名字打印为一个词。

cout <<"Your full name is: "<< fname << ' '  << lname << endl;

将 b 声明为 0 时,您没有将 a 分配给任何东西。

int a = 0, b = 0;

并且 cout 比 printf 强大得多,除非没有其他选择,否则不应在 C++ 中使用 C 语法。

cout << a << ' ';

cout  << "\nNos of counts " << b;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM