Before I go any further, I just want say yes, this is the same code from my last post, and no this isn't a repost. Now that I know that I need a loop in my code, all I need to know right now is where to put the loop. The only reason I'm not asking this in my previous thread is that everyone seemed to have lost interest in that one.

As I said before in this thread, and in my previous thread, all I need to know right now is where in my code do I need to put a loop.

#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
using namespace std;

int main()
    {
    //declare array
    int temp[7][2]= {0};
    int day = 0;
    const int lowtemp = 0;
    const int hightemp = 1;

    //enter data into array
    cout<<endl<<"Enter Day: ";
    cin >> day;
    cout << "\nEnter low temperature: ";
    cin >> temp[day][lowtemp];
    cout << "\nEnter high temperature: ";
    cin >> temp[day][hightemp];

    //test
    cout << "\nDay: " << day << " High: " << temp[day][hightemp] << " Low: " << temp[day][lowtemp] <<endl;

    system("pause");
    return 0;
    } //end of main function

Where do you think it should be? Note that there are 7 slots in the array for the day of the week, but you are only stuffing one day. So, what about a loop around the "Enter Day" and the test output section?

Okay, getting somewhere now. I now this isn't right, but am I at least on the right track?

#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
using namespace std;

int main()
    {
    //declare array
    int temp[7][2]= {0};
    int day = 0;
    const int lowtemp = 0;
    const int hightemp = 1;

    //enter data into array
    cout<<endl<<"Enter Day: ";
    cin >> day;
    cout << "\nEnter low temperature: ";
    cin >> temp[day][lowtemp];
    cout << "\nEnter high temperature: ";
    cin >> temp[day][hightemp];

    //begin loop
    Total = temp[0][hightemp] + temp[1][hightemp] + temp[2][hightemp] + temp[3][hightemp] + temp[4][hightemp] + temp[5][hightemp] + temp[6][hightemp]
    Total = temp[0][lowtemp] + temp[1][lowtemp] + temp[2][lowtemp] + temp[3][lowtemp] + temp[4][lowtemp] + temp[5][lowtemp] + temp[6][lowtemp]

    //test
    cout << "\nDay: " << day << " High: " << temp[day][hightemp] << " Low: " << temp[day][lowtemp] <<endl;

    system("pause");
    return 0;
    } //end of main function

Where do I go from here? What's the next step? What else do I need to do? Could someone please tell me something? I need this by tonight.

// Line#28 ( Go figure it out How to adapted in your code )
// The loop goes while 'i' < '7', and 'i' increases by one every loop

  for ( int i = 0; i < 7; i++ ) {
  cout<< temp[i] <<endl;
}

first of all i dont know that why are you not using array for storing days

try this(without making day as array)

 //declare array
    int temp[7][2]= {0};
    int day = 0;
    int i,aTotal,bTotal;
    aTotal=bTotal=0;
    const int lowtemp = 0;
    const int hightemp = 1;
    //enter data into array
    for(i=0;i<7;i++)
    {
    cout<<endl<<"Enter Day: ";
    cin >> day;
    cout << "\nEnter low temperature: ";
    cin >> temp[day][lowtemp];
    cout << "\nEnter high temperature: ";
    cin >> temp[day][hightemp];
    }
    //begin loop
    for(i=0;i<7;i++)
    {
    aTotal += temp[i][hightemp];
    bTotal += temp[i][lowtemp];
    }
    //test
    for(i=0;i<7;i++)
    cout << "\nDay: " << i << " High: " << temp[i][hightemp] << " Low: " << temp[i][lowtemp] <<endl;

at last dont forget to print aTotal and bTotal to ensure that the code producing result correctly
hope this helps you . . .

Edited 3 Years Ago by rishif2

Seeing as you're taking user input for the day, what constitutes a valid day? If I enter 3 for each day, what happens to every other entry in the array? What happens if I enter day values far beyond the bounds of the array? How about non numeric input?

If you're taking user input, you must validate the input and handle erroneous input.

This article has been dead for over six months. Start a new discussion instead.