Question

C++ class 3.2.4: If-else statement: Fix errors. Re-type the code and fix any errors. The code...

C++ class

3.2.4: If-else statement: Fix errors.

Re-type the code and fix any errors. The code should convert non-positive numbers to 1.

if (userNum > 0)
   cout << "Positive." << endl;
else
   cout << "Not positive, converting to 1." << endl;



Answer
#include 
using namespace std;

int main() {
   int userNum;

   cin >> userNum;

   /* Your solution goes here  */
   return 0;
   userNum = 1;
 
cout << "Final: " << userNum << endl;
0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>
using namespace std;

int main() {
       int userNum;

       cin >> userNum;

       if (userNum > 0)
          cout << "Positive." << endl;
   else{
          cout << "Not positive, converting to 1." << endl;
          userNum = 1;
       }
   
   cout << "Final: " << userNum << endl;

       return 0;
}
Add a comment
Know the answer?
Add Answer to:
C++ class 3.2.4: If-else statement: Fix errors. Re-type the code and fix any errors. The code...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ Fix the errors in the following code. (Not all errors are syntax related) #include <iostream>...

    C++ Fix the errors in the following code. (Not all errors are syntax related) #include <iostream> using namespace std; int main() { //Part A int numA numA = 10; cout << numA << end; /* Part B */*/ int numB = numA; cin >> usrInput; int usrInput = 0; numB = numB + usrInput; cout << numB <"/n";    /* Part C */ int numC = 10000000000; cout << numC << endl; return 0; }

  • Find and fix the errors in this C++ code: * This program illustrates a variety of...

    Find and fix the errors in this C++ code: * This program illustrates a variety of common loop errors. * Fix the errors in each section. */ #include <iostream> using namespace std; int main() { cout << "Welcome to Loop World" << endl; // SECTION I: update comment below on how you fixed this section's code, and tests run // FIX = // TESTS: cout << endl; cout << "******************" << endl; cout << "Section I" << endl; cout <<...

  • A) Fix any errors to get the following program to run in your environment.               B)...

    A) Fix any errors to get the following program to run in your environment.               B) Document each line of code with comments and describe any changes you had to make to the original code to get it to work. C) Write a summary of what your final version of the program does. You may also add white space or reorder the code to suit your own style as long as the intended function does not change. Program 3 #include...

  • 15.6: Fix the code to print the count from 1 to x (to loop x times)...

    15.6: Fix the code to print the count from 1 to x (to loop x times) #include <iostream> // include the header file using namespace std; void count(int x){    for(int i=1; i<=x; i++){ cout<<x; } } int main(){    int x,i;    cin >> x; count(x);    return 0; } 15.7 Fix the nested for loops to print the count from 1 to x twice, e.g. "12....x12.....x" #include <iostream> // include the header file using namespace std; int main(){...

  • C++ Write a function so that the main() code below can be replaced by the simpler...

    C++ Write a function so that the main() code below can be replaced by the simpler code that calls function MphAndMinutesToMiles(). Original main(): int main() { double milesPerHour; double minutesTraveled; double hoursTraveled; double milesTraveled; cin >> milesPerHour; cin >> minutesTraveled; hoursTraveled = minutesTraveled / 60.0; milesTraveled = hoursTraveled * milesPerHour; cout << "Miles: " << milesTraveled << endl; return 0; } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include <iostream> using...

  • The code will not run and I get the following errors in Visual Studio. Please fix the errors. err...

    The code will not run and I get the following errors in Visual Studio. Please fix the errors. error C2079: 'inputFile' uses undefined class 'std::basic_ifstream<char,std::char_traits<char>>' cpp(32): error C2228: left of '.open' must have class/struct/union (32): note: type is 'int' ): error C2065: 'cout': undeclared identifier error C2065: 'cout': undeclared identifier error C2079: 'girlInputFile' uses undefined class 'std::basic_ifstream<char,std::char_traits<char>>' error C2440: 'initializing': cannot convert from 'const char [68]' to 'int' note: There is no context in which this conversion is possible error...

  • CGALLENE 4.2: More syntax errors. ACTIVITY Each cout statement has a syntax error. Type the first...

    CGALLENE 4.2: More syntax errors. ACTIVITY Each cout statement has a syntax error. Type the first cout statement, and press Run to observe the error message. Fix the error, and run again. Repeat for the second, then third, cout statement cout << "Num: "<< songnum << endl; cout << int songNum << endl; cout << songNum " songs" << endl; Note: These activities may test code with different test values. This activity will perform two tests: the first with songNum-5,...

  • Fix this code so only the function prototype comes before main. #include <iostream> using namespace std;...

    Fix this code so only the function prototype comes before main. #include <iostream> using namespace std; bool isMultiple(int num1, int num2) { return num1 % num2 == 0; } int main() { char ch = 'Y'; int num1, num2; while(ch =='Y') // While ch is equal to Y { cout << "Enter two numbers(largest first): "; cin >> num1; // Getting 1st number cin >> num2; // Getting 2nd number if(isMultiple(num1, num2)) cout << num2 << " " << "IS...

  • Fix the following code to print whether the input is positive or negative #include <iostream> #include...

    Fix the following code to print whether the input is positive or negative #include <iostream> #include <string> using namespace std; void positiveOrNegative(int); // declare function int main(){    int y; int result; cin >> y; result = positiveOrNegative(y);    return 0; } void positiveOrNegative(int x){ if (x>=0) cout << "Positive"; else cout << "Negative"; }

  • Question 1: Fix the 2D dynamic array initialization in following code #include <iostream> using namespace std;...

    Question 1: Fix the 2D dynamic array initialization in following code #include <iostream> using namespace std; int main(){    int rows = 5; int cols = 5; int x;    int** arr = new int[rows][cols]    cin >> x; arr[x][x] = x; cout << "arr[x][x] = " << arr[x][x];    return 0; } Question 2: Fix the code to initialize the 2D array elements to x #include <iostream> using namespace std; int main(){    int rows; int cols; int x;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT