logo
down
shadow

C++ homework (while loop termination)


C++ homework (while loop termination)

By : Sandra Salamanca
Date : November 17 2020, 11:58 AM
wish of those help Instead of reading val1 and val2 directly from stdin, read a line of text. If the line does not start with |, extract the numbers from the line using sscanf or istringstream. If the line starts with |, break out of the while loop.
code :
#include <stdio.h>
#include <iostream>

using namespace std;

const int LINE_SIZE = 200; // Make it large enough.

int main()
{
   char line[LINE_SIZE] = {0};
   int val1=0, val2=0;

   // Break out of the loop after reading a line and the first character
   // of the line is '|'.
   while( true )
   {
      cout << "Enter 2 integers, teminate with \"|\"" << endl;

      // Read the entered data as a line of text.
      cin.getline(line, LINE_SIZE);
      if ( !cin )
      {
         // Deal with error condition.
         break;
      }

      // If the first character of the line is '|', break out of the loop.
      if ( line[0] == '|' )
      {
         break;
      }

      // Read the numbers from the line of text.
      int n = sscanf(line, "%d %d", &val1, &val2);
      if ( n != 2 )
      {
         // Deal with error condition.
         continue;
      }

      cout << val1 << " " << val2 << "\n\n";
   }

   return 0;
}


Share : facebook icon twitter icon
why does statement below loop executes before termination of loop

why does statement below loop executes before termination of loop


By : user2987938
Date : March 29 2020, 07:55 AM
will be helpful for those in need Well it is not executing before loop, it is executing correctly.
Your console may be refreshing little late.
I am thinking about for loop termination is it halts the loop or what's the actual story

I am thinking about for loop termination is it halts the loop or what's the actual story


By : Bob O
Date : March 29 2020, 07:55 AM
To fix this issue , You need to put your code within the for block. Remove the semicolon
code :
For(i=1;i<=num;i++)

{
// code here
}
Infinite loop in homework

Infinite loop in homework


By : Китайская Электроник
Date : March 29 2020, 07:55 AM
hop of those help? Your loop condition is constant, so either the loop is not executed or it keeps executing forever. Change to code to use a loop is an acceptable number of cycles is requested:
code :
#include <stdio.h>

void PrintShampooInstructions(int numCycles) {
    if (numCycles < 1) {
        printf("Too few.\n");
        return;
    } else
    if (numCycles > 4) {
        printf("Too many.\n");
        return;
    } else {
        int N = 1;
        while (N <= numCycles) {
            printf("%d: Lather and rinse.\n", N);
            ++N;
        }
        printf("Done.\n");
        return;
    }
}

int main(void) {
    PrintShampooInstructions(2);
    return 0;
}
For loop homework

For loop homework


By : Óscar Castro
Date : March 29 2020, 07:55 AM
I hope this helps you . I have homework and these are my instructions: , You need to make use of both earlier and later in your loop:
code :
function addThemUp(earlier,later) {

  var total = 0;

  for (i = earlier; i <= later; i ++) {

      total = total + i;

  };

  return total;
};
Can a termination variable in a condition of for loop be changed inside the loop?(Java)

Can a termination variable in a condition of for loop be changed inside the loop?(Java)


By : Alexander Budanov
Date : March 29 2020, 07:55 AM
this will help The string length gets calculated every loop, and your for could throw an IndexOutOfBoundsException if your string becomes too short.
IMHO yours is a very bad practice, for loops are intended to make a determined number of loops and should never be stopped, also their stop condition should never be changed inside the loop, you should use a while if you don't know how many iterations you want to do. But this is my personal opinion :)
Related Posts Related Posts :
  • Stuck when testing custom list class in c++
  • Using each member of class within one function to calculate average
  • check whether if two squares are intersecting with each other
  • Glm Quaternion lookat function
  • Is there guarantee heap allocated block address will not change(implicitly)?
  • Cosine Calculation without cmath library
  • Manually deleting a pointer returned by function
  • Avoid output interleaving
  • C++ error : Expected an identifier
  • Segmentation fault when I call operator new in linux mint
  • Recursively Solving A Sudoku Puzzle Using Backtracking Theoretically
  • lambda closure type and default argument in function template
  • How to ensure the function return consistent floating point values in C/C++?
  • What does throw do when not in used with try and catch?
  • The Unpredictablilty of the Order of Evaluation of Subexpressions
  • cannot convert const wchar_t to 'const_char' for argument 2 in MessageBOX etc etc
  • Size of a empty Class & Derived Virtual Class
  • unformatted i/o to and from memory
  • Storing an std::thread object as a class member
  • Remove duplicate string in an array
  • How to use copy_if() polymorphically?
  • How to organize a class's member functions?
  • How to wrap std::chrono in a c++ iterator type
  • Check if a socket is connected in mac os x
  • Grabbing the mouse cursor in GLFW
  • Calling Windows shell menu (same as right-click in Explorer) for multiple files programmatically
  • Pop up dialog to save file in seperate thread
  • Why would you use the keyword const if you already know variable should be constant?
  • Detecting Small Sound Effects In C++
  • How to avoid the copy when I return
  • Getting error code C2228: left of '._Ptr' must have class/struct/union
  • In win32 C++ programming how can I duplicate a window style?
  • Should the order of import statements matter when importing a .so?
  • Design decision regarding std::array fill
  • Access array of C++ structs from Fortran?
  • Determining when functions allocate memory in C++
  • C++ using std::set remove duplicate item for a struct fail.
  • Code to find out the number of triplets which lead to a sum less than or equal to threshold is giving a wrong output
  • Converting glm::lookat matrix to quaternion and back
  • Searching multi-dimensional vectors
  • Send and receive via SOCKS5 c++
  • GDI+: unhandled exception when drawing bitmap
  • Editing other processes memory
  • Object deleted when its member function is being executed?
  • Infinite Loop while inputting the different data type values in STACK
  • const members and operator=
  • Threaded Video Player sync
  • Does infinite of floating point number satisfy these equation?
  • map sorting in c++ by the frequencies
  • The Preprocessor program linked in g++ similar to the cpp program in gcc
  • How is the union used in this OpenCV library?
  • C++ memory leak in recursion
  • C++ Error: C4430 and C2143 Error at an impossible place
  • How can I track object lifetime in C++11 lambda?
  • #include statement mapping in Biicode (biicode.conf)
  • std::equal gives "Term doesnt evaluate to a function taking 2 arguments"
  • C++ template argument as reference lvalue
  • Legal to forward declare C standard library entities but not C++ standard library entities?
  • Conversion of wchar_t* to string
  • VirtualTreeView - Embarcadero C++ Builder XE7 - 64 bits
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org