我是C ++的新手,我想制作一个“编码器”(与其他编码器不同)。我发了一篇新文章,因为我听不懂其他所有文章,而且我是C ++的新手。我不知道如何使用scanf
或cin.getline
。这是我当前的代码:
#include <iostream>
#include <programConfig.cpp>
#include <windows.h>
using namespace std;
int main(){
for(int times=1;times!=0;times++){
char option='0', userText;
string changed=" ";
cout<<"Please enter your option (1=Encode, 2=Decode): ";
cin>>option;
if(option=='1'){
system("cls");
cout<<"Enter a string. Type any character other than A-Z to stop: ";
for(int times=1;times!=0;times++){
cin>>userText;
if(userText=='a' || userText=='A'){
changed.append("q");
}else if(userText=='b' || userText=='B'){
changed.append("n");
}else if(userText=='c' || userText=='C'){
changed.append("s");
}else if(userText=='d' || userText=='D'){
changed.append("j");
}else if(userText=='e' || userText=='E'){
changed.append("k");
}else if(userText=='f' || userText=='F'){
changed.append("u");
}else if(userText=='g' || userText=='G'){
changed.append("l");
}else if(userText=='h' || userText=='H'){
changed.append("o");
}else if(userText=='i' || userText=='I'){
changed.append("p");
}else if(userText=='j' || userText=='J'){
changed.append("w");
}else if(userText=='k' || userText=='K'){
changed.append("i");
}else if(userText=='l' || userText=='L'){
changed.append("a");
}else if(userText=='m' || userText=='M'){
changed.append("h");
}else if(userText=='n' || userText=='N'){
changed.append("5");
}else if(userText=='o' || userText=='O'){
changed.append("1");
}else if(userText=='p' || userText=='P'){
changed.append("3");
}else if(userText=='q' || userText=='Q'){
changed.append("2");
}else if(userText=='r' || userText=='R'){
changed.append("4");
}else if(userText=='s' || userText=='S'){
changed.append("6");
}else if(userText=='t' || userText=='T'){
changed.append("9");
}else if(userText=='u' || userText=='U'){
changed.append("7");
}else if(userText=='v' || userText=='V'){
changed.append("8");
}else if(userText=='w' || userText=='W'){
changed.append("?");
}else if(userText=='x' || userText=='X'){
changed.append("@");
}else if(userText=='y' || userText=='Y'){
changed.append("!");
}else if(userText=='z' || userText=='Z'){
changed.append("#");
}else if(userText==' '){
changed.append("=");
}else if(userText=='?'){
changed.append("(");
}else if(userText=='!'){
changed.append(")");
}else{
break;
}
}
cout<<"Encoded string:"<<changed<<"."<<endl;
pressEnter("");
system("cls");
}else if(option=='2'){
system("cls");
cout<<"Enter an encoded string. Type any character other than A-Z to stop: ";
for(int times=1;times!=0;times++){
cin>>userText;
if(userText=='q' || userText=='Q'){
changed.append("a");
}else if(userText=='n'){
changed.append("b");
}else if(userText=='s'){
changed.append("c");
}else if(userText=='j'){
changed.append("d");
}else if(userText=='k'){
changed.append("e");
}else if(userText=='u'){
changed.append("f");
}else if(userText=='l'){
changed.append("g");
}else if(userText=='o'){
changed.append("h");
}else if(userText=='p'){
changed.append("i");
}else if(userText=='w'){
changed.append("j");
}else if(userText=='i'){
changed.append("k");
}else if(userText=='a'){
changed.append("l");
}else if(userText=='h'){
changed.append("m");
}else if(userText=='5'){
changed.append("n");
}else if(userText=='1'){
changed.append("o");
}else if(userText=='3'){
changed.append("p");
}else if(userText=='2'){
changed.append("q");
}else if(userText=='4'){
changed.append("r");
}else if(userText=='6'){
changed.append("s");
}else if(userText=='9'){
changed.append("t");
}else if(userText=='7'){
changed.append("u");
}else if(userText=='8'){
changed.append("v");
}else if(userText=='?'){
changed.append("w");
}else if(userText=='@'){
changed.append("x");
}else if(userText=='!'){
changed.append("y");
}else if(userText=='#'){
changed.append("z");
}else if(userText=='='){
changed.append(" ");
}else if(userText=='('){
changed.append("?");
}else if(userText==')'){
changed.append("!");
}else{
break;
}
}
cout<<"Decoded string:"<<changed<<"."<<endl;
pressEnter("");
system("cls");
}
}
pressEnter("An error occurred that caused the program to jump out of all loops.");
return 0;
}
这是programConfig.cpp的代码:
#include <iostream>
#include <cstdlib>
using namespace std;
int power(int baseNumber, int powerNumber){
int result = 1;
for(int times=0;times<powerNumber;times++){
result = result * baseNumber;
}
cout<<result<<endl;
}
int cube(int cubeNumber){
cout<<cubeNumber*cubeNumber*cubeNumber<<endl;
}
int add(int firstNumber, int secondNumber){
cout<<firstNumber+secondNumber<<endl;
}
int subtract(int firstNumber, int secondNumber){
cout<<firstNumber-secondNumber<<endl;
}
int multiply(int firstNumber, int secondNumber){
cout<<firstNumber*secondNumber<<endl;
}
int divide(int firstNumber, int secondNumber){
cout<<firstNumber/secondNumber<<endl;
}
int pressEnter(string errorMessage){
if (errorMessage==""){
cout<<"Press Enter to continue."<<endl;
cin.ignore(10,'\n');
cin.get();
}else{
cout<<errorMessage<<endl;
cin.ignore(10,'\n');
cin.get();
}
}
int printText(string text){
cout<<text<<endl;
}
但是只要用户输入空格,cin
函数就会跳过它。
还有其他功能可以获取用户输入吗?我是C ++的新手,所以请清楚解释...