[英]How do I take input from the user of their full name, and then output their first name and last name seperately? (C++)
[英]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.