comparing int to newline character

This is a discussion on comparing int to newline character within the C++ Programming forums, part of the General Programming Boards category; Hi, I am trying to figure out how to write a program that will allow the user to enter as ...

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

    comparing int to newline character

    Hi, I am trying to figure out how to write a program that will allow the user to enter as many numbers as they want, and store them in a vector<t>. I am using a FOR () loop to accept the input (since I wouldn't know beforehand how many numbers they might want to input). To break out of the loop I am using an if statement that I want to check for the newline character. Of course, the input type is integer, and I recognize that the newline is type character so I am not totally shocked that this isn't working. I just don't know what to do about it. I did verify that if the loop would end, the rest of the code does what I want it to (by using a counting loop). So my question is, what syntax might I use to get the FOR loop to break when the 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 by spaces such as 987 393 903\n";
    
    	for(;;)
    	{
    		cin >> bigNum;
            if (bigNum == '\n') break;  // This is not breaking when "enter" is pressed.
    		num1.push_back(bigNum);
    	
    	}
        
    	cout << "You entered: ";
    	for (unsigned i = 0; i < num1.size(); i++)
    		cout << num1[i] << endl;
    
    }
    Semper Fi!

  2. #2
    Registered User
    Join Date
    May 2004
    Posts
    127
    Integers can be compared with character literals. The problem is that cin's >> operator is delimited by whitespace. It won't read a newline. A better way to go about this would be to use getline and strings, then break them up using 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 such as 987 393 903\n";
      getline(cin, line);
      stringstream sin(line);
      int 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
    Posts
    49
    Thats an interesting idea...but eventually I want to do some math with the numbers (I am working on a big-integer calculator). Can I still perform math on the input, if I use the getline method?
    Semper Fi!

  4. #4
    Registered User
    Join Date
    May 2004
    Posts
    127
    Quote Originally Posted by RedZippo
    Thats an interesting idea...but eventually I want to do some math with the numbers (I am working on a big-integer calculator). Can I still perform math on the input, if I use the getline method?
    Of course. The num1 vector still contains integers, you're just using an intermediate step in acquiring them to make life easier. getline reads a line from standard input, and sin converts the numbers represented as strings to integers. As before, those integers are pushed onto the vector and the end result is the same as before, except now it works.

  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 about "carrying" from one index to the next? I have it now where the user enters bigNum1, and then enters bigNum2, and I am going to add these together, element by element. But, what if the sum of an element (like 500 + 500) makes more numbers than what was there? For example, if I added 500, 500 + 500, 600 that should equal 1,001,100. But if I simply add each element, and then display them I will get 10,001,100. So obviously I need a method to determine when a number should be carried, and a way to carry it. Got any idea's?
    Semper Fi!

  6. #6
    Registered User
    Join Date
    May 2004
    Posts
    127
    Quote Originally Posted by RedZippo
    Indeed it does work! You are great!

    Do you know anything about how I should go about "carrying" from one index to the next? I have it now where the user enters bigNum1, and then enters bigNum2, and I am going to add these together, element by element. But, what if the sum of an element (like 500 + 500) makes more numbers than what was there? For example, if I added 500, 500 + 500, 600 that should equal 1,001,100. But if I simply add each element, and then display them I will get 10,001,100. So obviously I need a method to determine when a number should be carried, and a way to carry it. Got any idea's?
    You want to work through several test cases of varying complexity. Look for patterns in how one number carries over into the next. This should give you an idea of how to begin developing your algorithm.
    When writing a specialization, be careful about its location; or to make it compile will be such a trial as to kindle its self-immolation.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  3. Converted from Dev-C++ 4 to Dev-C++ 5
    By Wraithan in forum C++ Programming
    Replies: 8
    Last Post: 12-03-2005, 06:45 AM
  4. Switch/case Problems (long code in post)
    By Wraithan in forum C++ Programming
    Replies: 2
    Last Post: 12-01-2005, 05:40 PM
  5. A Simple (?) Problem
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 10-12-2001, 04:28 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21