-1

I've read and re-read the code but I can't find a logical conclusion as to why when run, there isn't a newline created between the start and end times. Both positive and negative advice is appreciated.

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {
    //start time and end time of shift
    vector <int> vstart;
    vector <int> vend;
    vector <string> days_of_week = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
    int start, end;
    while (cin >> start) {
        vstart.push_back(start);
    }
    while (cin >> end) {
        vend.push_back(end);
    }

    for (string d : days_of_week) {
        cout << d << "\t";
    }
    cout << endl << "---------------------------------------------------------\n";
    for (int s : vstart) {
        cout << s << "\t";
    }
    cout << endl;
    for (int e : vend) {
        cout << e << "\t";
    }
    cout << endl;
}    
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
NewToThis
  • 9
  • 3
  • 2
    For some specified input, can you please show us the expected and actual output? – Some programmer dude Jun 29 '17 at 06:57
  • You should use `for(const string& d: days_of_week)` if you wanna avoid redundant copy operation. – voltento Jun 29 '17 at 06:57
  • The output should list the days of the week as defined in days_of_week separated by tabs followed by a barrier of dashes then a newline and the start times of each shift followed by a newline and the end times of each shift – NewToThis Jun 29 '17 at 06:58
  • 2
    I'm pretty sure you have no `end` times and the first `while` puts everything into `vstart`; you can verify that easily with a debugger. How does your input look like? – Bartek Banachewicz Jun 29 '17 at 06:58
  • Yeah, the first `while` loop will execute as long as it can successfully extract `int` values. It's consuming all the input before you reach the second `while` loop. – Blastfurnace Jun 29 '17 at 07:00
  • @BartekBanachewicz that seems to make sense. I'm new to this as my name entails. I also tried using an && statement such as while (cin >> start && end) but it also didn't seem to work. – NewToThis Jun 29 '17 at 07:01
  • Perhaps you should use a standard `for` loop for the input of the start and end times, to make sure you only get seven elements of each? – Some programmer dude Jun 29 '17 at 07:02
  • 1
    Also, please *show* (by editing the question) the input and the outputs. Copy-paste it from the terminal. Please take some time to [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask). And also please read [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) by Eric Lippert. – Some programmer dude Jun 29 '17 at 07:03
  • @Someprogrammerdude that is a very good suggestion, I'll learn from this mistake and include what kind of inputs I am making as well as the outputs I am expecting. Thank you guys very much for your kind words. – NewToThis Jun 29 '17 at 07:04

2 Answers2

1

Let's look at this portion of code.

while (cin >> start) {
    vstart.push_back(start);
}
while (cin >> end) {
    vend.push_back(end);
}

In the first loop, you read in values until cin>>start reaches an end-of-file byte, or fails in a different way. But you do not clear that fail-state. You have to call cin.clear(); to be able to read new input in the second loop.

while (cin >> start) {
    vstart.push_back(start);
}
cin.clear();
while (cin >> end) {
    vend.push_back(end);
}

Further reading: Why would we call cin.clear() and cin.ignore() after reading input?

HeavensInc
  • 78
  • 6
1

This while loop will execute as long as it can successfully extract int values. It doesn't know to stop after reading 7 numbers and is putting all your input into vstart.

while (cin >> start) {
    vstart.push_back(start);
}

I think you want something like this for loop that includes logic to stop after reading 7 values.

for (int i = 0; (i < 7) && (cin >> start); ++i) {
    vstart.push_back(start);
}
Blastfurnace
  • 18,411
  • 56
  • 55
  • 70