[a / b / c / d / e / f / g / gif / h / hr / k / m / o / p / s / t / u / v / vg / vm / vmg / vr / vrpg / vst / w / wg] [i / ic] [r9k / s4s / vip] [cm / hm / lgbt / y] [3 / aco / adv / an / bant / biz / cgl / ck / co / diy / fa / fit / gd / hc / his / int / jp / lit / mlp / mu / n / news / out / po / pol / pw / qst / sci / soc / sp / tg / toy / trv / tv / vp / vt / wsg / wsr / x / xs] [Settings] [Search] [Mobile] [Home]
Board
Settings Mobile Home
/g/ - Technology


Thread archived.
You cannot reply anymore.


[Advertise on 4chan]


File: snailcat.jpg (26 KB, 447x447)
26 KB JPG
I recently started learning c++ and wrote this code myself but something's off the loop break doesn't seem to terminate the program.
can someone take a look?
#include <iostream>
// input output

double add (double a,double b){
return a + b;
}
// add function
double subtract (double a,double b){
return a - b;
}
//subtract function
double multiply (double a,double b){
return a * b;
}
//multiply function
double divide (double a,double b){
if (b == 0){
return 0;
}
return a / b;
}

//divide function
int main(){
char option = ' ';
double a, b;

// main loop
std::cout<<"Choose an operation(+ - * / q)" <<std::endl;
while(option != 'q'){


std::cin>>option;
if(option == 'q'){
break;
}
// quit
std::cout<<"enter the first number" << std::endl;
std::cin>>a;
std::cout<<"enter the second number" << std::endl;
std::cin>>b;
//option


if(option == '+'){
std::cout<<add(a, b) << std::endl;
}
else if(option == '-'){
std::cout<<subtract(a, b) << std::endl;
}
else if(option == '*'){
std::cout<<multiply(a, b) << std::endl;
}
else if(option == '/'){
std::cout<<divide(a, b) << std::endl;
}
else{std::cout<<"invalide operation"<< std::endl;}


}
return 0;
//end
}

I refuse to seek claud's help. I'd be a snail cat
>>
why do you need to break instead of just returning?
>>
>>109250238
what?
>>
>>109250260
Returning from main exits the program.
>>
File: 1765727115778812.mp4 (3.53 MB, 990x720)
3.53 MB
3.53 MB MP4
>>109250180
use AI
>>
>>109250180
does the program actually enter the if block? maybe the comparison doesn't work the way you think.
use a debugger or print something inside the if block.
>>
>>109250289
Use AI to generate a black female with massive tits smothering a small frail white female with said tits
>>
"break" doesn't instantly end the loop, "continue" does, but you dont have to use break or continue here at all, just return
(or make a goto to the return if you want it to be funny)
>>
>>109251188
>"break" doesn't instantly end the loop, "continue" does
idiot
>>
>>109251188
continue just skips to the next iteration, break breaks the loop
>>
Everyone! I fixed it!
#include <iostream>
// input output

double add (double a,double b){
return a + b;
}
// add function
double subtract (double a,double b){
return a - b;
}
//subtract function
double multiply (double a,double b){
return a * b;
}
//multiply function
double divide (double a,double b){
if (b == 0){
return 0;
}
return a / b;
}

//divide function
int main(){
char option = ' ';
double a, b;

// main loop
while(option != 'q'){
std::cout<<"Choose an operation(+ - * / q)" <<std::endl;

std::cin>>option;
if(option == 'q'){
break;
}
// quit
std::cout<<"enter the first number" << std::endl;
std::cin>>a;
std::cout<<"enter the second number" << std::endl;
std::cin>>b;
//option

std::cout<<"The result is:" <<std::endl;
// print

if(option == '+'){
std::cout<<add(a, b) << std::endl;
}
else if(option == '-'){
std::cout<<subtract(a, b) << std::endl;
}
else if(option == '*'){
std::cout<<multiply(a, b) << std::endl;
}
else if(option == '/'){
std::cout<<divide(a, b) << std::endl;
}
else{std::cout<<"invalide operation"<< std::endl;}


}
return 0;
//end
}



[Advertise on 4chan]

Delete Post: [File Only] Style:
[Disable Mobile View / Use Desktop Site]

[Enable Mobile View / Use Mobile Site]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.