简体   繁体   English

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

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

I know this is something really simple but I can't figure out what I'm missing or doing wrong.我知道这很简单,但我无法弄清楚我错过了什么或做错了什么。 I think it might be something with the char variables.我认为这可能与 char 变量有关。 This is what I have so far:这是我到目前为止:

#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";

You are trying to extract into the string literal "fname" .您正在尝试提取到字符串文字"fname" It seems you meant to extract into the variable fname :看来您打算提取到变量fname

cin>>fname;

Just going to make a couple suggestions since Joseph answered the question.自从约瑟夫回答了这个问题后,我只想提出一些建议。 Add a space between first and last name so it's not printing there name as one word.在名字和姓氏之间添加一个空格,这样它就不会将名字打印为一个词。

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

when declaring b to 0, you didn't assign a to anything.将 b 声明为 0 时,您没有将 a 分配给任何东西。

int a = 0, b = 0;

and cout is much more powerful than printf, you shouldn't use C syntax in C++ unless there is no other option.并且 cout 比 printf 强大得多,除非没有其他选择,否则不应在 C++ 中使用 C 语法。

cout << a << ' ';

and

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

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

相关问题 我如何从他们的全名的用户那里得到输入,然后 output 他们的名字和姓氏分开? (C++) - How do I take input from the user of their full name, and then output their first name and last name seperately? (C++) 从全名QString中获取名字和姓氏 - Get first name and last name out of a full name QString 当仅在一行上提示输入一次时,如何确保用户输入名字和姓氏? - How to ensure user inputs a first and last name when only prompting for input once on one line? 如何输出以姓氏开头的全名? 如果名字或姓氏由单词组成怎么办? - How output a full name that starts with last name? What if the the first name or last name consist of words? 在Windows服务器域中检索用户的名字和姓氏(给定其登录名) - Retrieve a user's First and Last Name, given its login name, inside a windows server domain 为什么不输出名字和姓氏? - Why is first and last name not outputting? 按姓氏和名字排序结构 - Sort a struct by last then first name 从文件读取名字和姓氏 - Reading First and Last Name from File 如何使用void函数读取名字和姓氏 - How to read first and last name with void function c++ - 如何拆分名字,中间名和姓氏,并将其打印为姓氏,首字母中间名首字母 - how to split the first, middle and last names, and print it as last name, first initial middle initial c++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM