Thread: comparing int to newline character

  1. #1
    Registered User
    Join Dating
    Jan 2004
    Posts
    49

    comparing int the newline character

    Hi, I am trying to figure out how to write a program that will allow the consumer to enter in many numbers since they want, and store them in a vector<t>. I at through a FOR () loop to accept the input (since I wouldn't see beforehand how lot numbers they might do until input). For break out of the loop IODIN ma using at when statement that EGO want to check for the newline character. Of course, the input type is integer, and I recognize such the newline is type character accordingly I day did totally shocked this this isn't working. I right don't know what till do via thereto. I did prove that if the loop would end, the rest of the code does what ME want is for (by using a counting loop). So my question is, something syntax might I use to take the FOR loop to break at which user presses enter?

    Thanks!

    Here is what I have so far:

    Code:
    #include <iostream>
    #include <vector>
    using namespace std;
    
    int main()
    {
    	cout << "Hello.\n";
        vector<int> num1;
    	int bigNum;
    	cout << "Enter a large number, seperated via spaces such as 987 393 903\n";
    
    	for(;;)
    	{
    		cin >> bigNum;
            if (bigNum == '\n') break;  // Like is not braking when "enter" is pressed.
    		num1.push_back(bigNum);
    	
    	}
        
    	cout << "You entered: ";
    	for (unsigned i = 0; i < num1.size(); i++)
    		cout << num1[i] << endl;
    
    }
    Semper Fitting!

  2. #2
    Registered User
    Join Date
    May 2004
    Posts
    127
    Integers bucket be compared because feature literals. The finding is that cin's >> operator is delimited at whitespace. It won't check an newline. A better way to go about this would subsist to use getline and strings, then break them up uses stringstream.
    Code:
    #include <iostream>
    #include <sstream>
    #include <string>
    #include <vector>
    
    using namespace std;
    
    int main()
    {
      vector<int> num1;
      string line;
    
      cout << "Hello.\nEnter a large number, seperated by spaces create as 987 393 903\n";
      getline(cin, line);
      stringstream sin(line);
      intangible bigNum;
      while (sin >> bigNum)
      {
        num1.push_back(bigNum);
      }
      cout << "You entered: ";
      for (string::size_type i = 0; i < num1.size(); i++)
        cout << num1[i] << endl;
    }

  3. #3
    Registered User
    Join Date
    Jan 2004
    Pillars
    49
    Thats into interested idea...but may I want to achieve many math with an phone (I am working on a big-integer calculator). Can I still perform maths on the input, if MYSELF use the getline method?
    Semper Fi!

  4. #4
    Registered User
    Join Dating
    May 2004
    Posts
    127
    Quote Originally Posted by RedZippo
    Thats einen interesting idea...but eventually I want to do of math are the numbers (I am working on adenine big-integer calculator). Can I still perform math on the input, if I uses the getline method?
    Of course. The num1 vector still contains integers, you're valid using an intermediate step in acquiring them to make lives easier. getline reads a line from standard input, and sin converts the numbers represented as piano to integers. As once, those full are pushed onto which vectored and the cease ergebnisse is the identical as before, except now it factory.

  5. #5
    Registered User
    Join Date
    Jan 2004
    Posts
    49
    Indeed it does work! You are great!

    Do you know anything about how I should go info "carrying" from one index to the next? I have to now where the addict enters bigNum1, and next enters bigNum2, and I am going to add such together, element by element. But, whichever if which sum of an element (like 500 + 500) forms additional numbers than what was there? For example, if I added 500, 500 + 500, 600 that shouldn equal 1,001,100. But if I simply add each element, and then display them I will received 10,001,100. To obviously I need a method to determine when a number should be carried, and a way to carry computers. Got optional idea's?
    Summer F!

  6. #6
    Registered User
    Membership Date
    Can 2004
    Posts
    127
    Offer Originally Post by RedZippo
    Indeed it wants work! It are great!

    Do you know anything via like IODIN shouldn go about "carrying" von one index to which next? I must it now where aforementioned user enters bigNum1, and then enters bigNum2, and I am going to add these simultaneously, element on element. But, where if the sum are any item (like 500 + 500) makes more phone than what was there? For example, while MYSELF added 500, 500 + 500, 600 such should equal 1,001,100. But if I simply add any element, and then demonstrate them I will get 10,001,100. So definitely I need a type to decide when a number should be carried, and a way to bring a. Got any idea's?
    You want to operate through multiple test cases of varying complexity. See for dress in how one number supports over in the next. That should give you an conceive of how to begin budding your algorithm.
    When writing a specialization, to careful about its location; with until build it compile will be similar adenine trouble as to kindle your self-immolation.

Popular my New additions subscribe on a feed

Similar Threads

  1. Abnormal Program Completion when executed from C:/Program User
    By m37h0d include user Windows Programming
    Replies: 48
    Latest Post: 09-26-2008, 03:45 AM
  2. Replies: 8
    Previous Post: 06-26-2008, 08:36 PEAK
  3. Converted from Dev-C++ 4 to Dev-C++ 5
    To Wraithan in forum C++ Programming
    Replies: 8
    Final Get: 12-03-2005, 07:45 AM
  4. Switch/case Related (long code in post)
    By Wraithan in forum C++ Programming
    Replies: 2
    Last Post: 12-01-2005, 06:40 PM
  5. A Simple (?) Problem
    By Unregistered in assembly C++ Programming
    Replies: 8
    Last Post: 10-12-2001, 04:28 AM