C++ 我不明白为什么我的循环在执行时不能正常工作,它还打印了不正确的选项cout #包括 #包括 #包括 使用名称空间std; int main() { int a,b,c,z,a1,b1,a2,b2;//变量的偏差 浮子l; 字符选择; 对于(;;) { cout
您的代码结构错误,您正在使用for循环来实现它们不是为之设计的 在do…while循环中使用switch语句,并使用一个布尔变量来告诉您何时退出C++ 我不明白为什么我的循环在执行时不能正常工作,它还打印了不正确的选项cout #包括 #包括 #包括 使用名称空间std; int main() { int a,b,c,z,a1,b1,a2,b2;//变量的偏差 浮子l; 字符选择; 对于(;;) { cout,c++,C++,您的代码结构错误,您正在使用for循环来实现它们不是为之设计的 在do…while循环中使用switch语句,并使用一个布尔变量来告诉您何时退出 #include <iostream> #include <conio.h> #include <math.h> using namespace std; int main() { int a,b,c,z,a1,b1,a2,b2; //decleration of variables float
#include <iostream>
#include <conio.h>
#include <math.h>
using namespace std;
int main()
{
int a,b,c,z,a1,b1,a2,b2; //decleration of variables
float l;
char opt;
for(;;)
{
cout << "-----Calculator----- " <<endl <<endl;
cout << "Press (a) to add two vectors \n";
cout << "Press (s) to subtract two vectors \n";
cout << "Press (m) to multiply two vectors \n";
cout << "Press (l) to calculate the length of two vectors \n";
cout << "Press (q) to quit \n"<<endl;
cout << "Enter your choice: \t";
cin>>opt;
//INPUT FOR ADDITION
for(;opt == 'a';)
{
cout<<"Enter first number of vactor"<<endl;
cin>>a1;
cout<<"Enter second number of vactor"<<endl;
cin>>a2;
cout<< "The vactor is"<<" :"<<"["<<a1<<","<<a2<<"]"<<endl;
cout<<endl;
//-------------------------------//
cout<<"enter the first number of vactor 2"<<endl;
cin>>b1;
cout<<"Enter the second number of vactor 2"<<endl;
cin>>b2;
//-------------------------------//
a=a1+b1; // Adding copmonent1 of 1st vector with component1 of 2nd vector
b=a2+b2; // Adding copmonent2 of 1st vector with component1 of 2nd vector
cout<<"The sum is"<<"["<<a<<","<<b<<"]"<<endl;
cout<<"Press any key to cont.... \n ";
//-------------------------------//
getch ();
break;
}
//INPUT FOR SUBTRACTION
for(;opt=='s';)
{
cout << "Enter 1st component of vector \n";
cin>>a1;
cout << "Enter 2nd component of vector \n";
cin>>a2;
cout << "The vector is \t : \t "<<"["<<a1<<","<<a2<<"]"<<endl;
//-------------------------------//
cout << "Enter 1st component of vector \n";
cin>>b1;
cout << "Enter 2nd component of vector \n";
cin>>b2;
cout << "The vector is \t : \t "<<"["<<b1<<","<<b2<<"]"<<endl;
//-------------------------------//
a=a1-b1; // Subtracting copmonent1 of 1st vector with component1 of 2nd vector
b=a2-b2; // Subtracting copmonent2 of 1st vector with component1 of 2nd vector
cout << "The difference is \t : \t " <<"["<<a<<","<<b<<"]"<<endl;
cout<<"Press any key to cont.... \n ";
//-------------------------------//
getch ();
break;
}
// INPUT FOR Multiply CALCULATION
for(;opt=='m';)
{
cout << "Enter 1st component of vector \n";
cin>>a1;
cout << "Enter 2nd component of vector \n";
cin>>a2;
cout << "The vector is \t : \t "<<"["<<a1<<","<<a2<<"]"<<endl;
//-------------------------------//
cout << "Enter 1st component of vector \n";
cin>>b1;
cout << "Enter 2nd component of vector \n";
cin>>b2;
cout << "The vector is \t : \t "<<"["<<b1<<","<<b2<<"]"<<endl;
//-------------------------------//
a=a1*b1; //Multiplying copmonent1 of 1st vector with component1 of 2nd vector
b=a2*b2; // Multiplying copmonent2 of 1st vector with component2 of 2nd vector
c=(a1*b1)+(a2*b2); // by formula of dot product used for vectors adding adding two products
cout<<"The multiplication is \t : \t " <<c<<endl;
cout<<"Press any key to cont.... \n ";
//-------------------------------//
getch ();
break;
}
// INPUT FOR LENGHTH CALCULATION
for(;opt=='l';)
{
cout << "Enter 1st component of vector \n";
cin>>a1;
cout << "Enter 2nd component of vector \n";
cin>>a2;
cout << "The vector is \t : \t "<<"["<<a1<<","<<a2<<"]"<<endl;
//-------------------------------//
// As the formula for length is sqrt(square of component 1 + square of component 2)
a=a1*a1; // Taking square of component 1
b=a2*a2; // Taking square of component 2
l=sqrt(a+b); //taking square root of (a+b) // Taking square root of result
cout<<"The length is \t : \t " <<l<<endl;
cout<<"Press any key to cont.... \n ";
//-------------------------------//
getch ();
break;
}
// Quit
for(;opt=='q';)
{
cout <<"Press any key to QUIT" << endl;
getch ();
return 0;
//-------------------------------//
}
//INCORRECT OPTION
cout<<"The option is incorrect. PLEASE enter the correct option";
cout<<endl;
}
return 0;
}
bool quit=false;
做
{
//菜单代码
cin>>opt;
开关(opt)
{
案例“a”:
//添加代码
打破
案例s:
//减法码
打破
...
案例‘q’:
退出=真;
打破
违约:
cout>opt;
如果(opt=='a')
{
//添加代码
}
else if(opt='s')
{
//减法码
}
...
else if(opt=='q')
{
退出=真;
}
其他的
{
不行,我们不知道你在寻求什么帮助,当你说“这不是我想要的,请修复它”解释你的程序有什么问题。输出是怎么错误的?输出是正确的,但当任何计算结束时,它也会在末尾打印错误的按键输入选项。这并不奇怪-这个输出周围没有条件,你期望什么?实际上我必须用这两个选项来编写程序我在do while中进行了更正,但坚持使用loop@user3073283我认为您的错误是在整个程序应该使用一个for循环的情况下,对每个选项使用一个for循环。用for循环替换do while循环,它应该可以工作。bool quit=false;for(;!quit;){…}
bool quit = false;
do
{
// menu code
cin>>opt;
switch (opt)
{
case 'a':
// adding code
break;
case 's':
// substracting code
break;
...
case 'q':
quit = true;
break;
default:
cout<<"The option is incorrect. PLEASE enter the correct option";
cout<<endl;
break;
}
} while (!quit);
out <<"Press any key to QUIT" << endl;
getch ();
return 0;
bool quit = false;
do
{
// menu code
cin>>opt;
if (opt == 'a')
{
// adding code
}
else if (opt == 's')
{
// substracting code
}
...
else if (opt == 'q')
{
quit = true;
}
else
{
cout<<"The option is incorrect. PLEASE enter the correct option";
cout<<endl;
}
} while (!quit);
out <<"Press any key to QUIT" << endl;
getch ();
return 0;