+ 4
Is my code wrong or has the app crash?
I'm doing project 3 of c++ (ticket) and every time I give him run any of the examples it randomly fails. This is the code: #include <iostream> using namespace std; int main() { int ages[5]; int menor = ages[0]; for (int i = 0; i < 5; ++i) { cin >> ages[i]; if (menor > ages[i]) { menor = ages[i]; } } float porcentaje = menor * 0.5; float final = 50 - porcentaje ; cout << final << endl; return 0; } Can someone explain to me if any part is wrong or why is this happening to me?
14 Respuestas
+ 6
you initialized 'menor' with an uninitialized value 'ages[0]'
I will suggest you to initialize it in the for loop, at the first iteration:
so add this in your for loop, after
cin >>ages[i];
if(i==0)
menor=ages[i];
+ 2
you initialized 'menor' with an uninitialized value 'ages[0]'
I will suggest you to initialize it in the for loop, at the first iteration:
so add this in your for loop, after
cin >>ages[i];
if(i==0)
menor=ages[i];
+ 1
I said after
cin>>ages[i];
not before! it should look like this:
cin>>ages[i];
if(i==0)
menor = ages[i];
....
+ 1
Oh, thanks. The code already works and I get it. Seriously thank you very much.
+ 1
you're welcome, keep practicing!
+ 1
Thank you, this code also works and it looks like the project was designed to be the result but the other one also served me.
+ 1
Helou
0
Thank you very much for the help but it has not solved the problem. Randomly some of the project examples continue to fail with numbers like 8.46362e+08.
0
can you share your code link?
- 1
<html lang="en" manifest="webogram.appcache" ng-csp="" xmlns:ng="http://angularjs.org" id="ng-app" style="display:background: rgb(231, 235, 240);">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<title>Telegram Web</title>
<link rel="stylesheet" href="css/app.css">
<link rel="manifest" href="manifest.webapp.json">
<link rel="icon" href="favicon.ico" type="image/x-icon">
<link rel="apple-touch-icon" href="img/iphone_home120.png">
<link rel="apple-touch-icon" sizes="120x120" href="img/iphone_home120.png">
<link rel="apple-touch-startup-image" media="(device-width: 320px)" href="img/iphone_startup.png">
<meta name="apple-mobile-web-app-title" content="Telegram Web">
<meta name="mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
<meta name="theme-color" content="#497495">
<m
- 2
<html lang="en" manifest="webogram.appcache" ng-csp="" xmlns:ng="http://angularjs.org" id="ng-app" style="display: block; background: rgb(231, 235, 240);">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<title>Telegram Web</title>
<link rel="stylesheet" href="css/app.css">
<link rel="manifest" href="manifest.webapp.json">
<link rel="icon" href="favicon.ico" type="image/x-icon">
<link rel="apple-touch-icon" href="img/iphone_home120.png">
<link rel="apple-touch-icon" sizes="120x120" href="img/iphone_home120.png">
<link rel="apple-touch-startup-image" media="(device-width: 320px)" href="img/iphone_startup.png">
<meta name="apple-mobile-web-app-title" content="Telegram Web">
<meta name="mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
<meta name="theme-color" content="#49749