-2

FYI, I declare a Class call UC, inside UC I declare a variables call course and its a array of [4], this related to the problem that I am facing right now. Go to the line that i comments as problem, all i know for now is that the line for(UC &i :: one.course) is wrong especially the UC , this line of code should do a forloop of course[4] but it doesnt, it just show error like i has not been declared. And my expected output is down there.

#include <iostream>
#include <string>

using namespace std;

class UC{
public:

    string name;
    int history;
    string founder;
    string course[4];
};

void print(string, int, string);

int main()
{


    UC one;
    one.name = "ABC";
    one.history = 5;
    one.founder = "Mr.Chong";
    one.course[0] = "IT";
    one.course[1] = "Interior Design";
    one.course[2] = "Mass Comm";
    one.course[3] = "Business";


    print(one.name, one.history, one.founder);
    cout<<"Our Course: ";

//problem here//
    string delim = "";

    for(UC &i :: one.course){
      cout<< delim <<i;
      delim = ", ";
    };   
//problem here//

    return 0;
}

void print(string r, int x, string y){
    cout<<"Our College Name: "<<r<<endl;
    cout<<"Our History: "<<x<<endl;
    cout<<"Our Founder: "<<y<<endl;
};

I expect the output will be like

Our College Name: ABC

Our History: 5

Our Founder: Mr.Chong

Our Course: IT, Interior Design, Mass Comm, Business

//this line doesnt works

Newbie
  • 13
  • 2
  • 1
    `string course[4];` is an array of 4 `string`s not `UC` – πάντα ῥεῖ Mar 30 '19 at 09:02
  • πάντα ῥεῖ Sry, can you explain it more details, i just started learning c++ fews days ago.. – Newbie Mar 30 '19 at 09:10
  • There are so many (trivial) errors in your code, I don't even know where to start to explain something to you. That's what [books and tutorials](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) are for, not questions at Stack Overflow. – πάντα ῥεῖ Mar 30 '19 at 09:12
  • Use one colon (`:`) instead of two (`::`) in a for-range loop. – TrebledJ Mar 30 '19 at 09:18
  • `one.course` has type `string[4]` (i.e. it's an array of 4 strings). `for (UC &i : one.course)` will throw an error since you're handling different types (you're trying to read values of type `UC` when the values are actually of type `string`. It should be `for (string &i : one.course)`. Even better: use `const string &i` since you're not modifying the variable or simply use `const auto &i`. – TrebledJ Mar 30 '19 at 09:27
  • `for(UC &i :: one.course)` is nonsense since `one.course` is not of type `UC` and the `::` doesn't make any sense either, hence the compilation errors. You probably intend something like `for (const auto &i : one.course)` or (without relying on type deduction) `for (const string &i: one.course)` (i.e. iterate over the elements of the array `one.course`. – Peter Mar 30 '19 at 09:39
  • Trebled J Thank you for your patient explanation! It answered my questions! => – Newbie Mar 30 '19 at 09:40
  • Thank you Peter, i just knowing that got this auto keyword function in c++ ! => – Newbie Mar 30 '19 at 09:52
  • Note that there is also [CodeReview](https://codereview.stackexchange.com/), on which you can have people improving your code in all sorts of manners. In any case, you should really learn how to ask questions. You begin with telling us that something does not work which we don't know about. Get a structure, like "I want this, I did this, this did not work as it should and instead did this". – Aziuth Mar 30 '19 at 10:53
  • Aziuth I am trying to make it clear, so does it clear for you now – Newbie Mar 30 '19 at 11:51

1 Answers1

1

Your problem section can be as below, to print out an array using for loop:

#include <iostream>
#include <string>

using namespace std;

class UC{
public:
    string name;
    int history;
    string founder;
    string course[4];
};

void print(string, int, string);

int main()
{
    UC one;
    one.name = "ABC";
    one.history = 5;
    one.founder = "Mr.Chong";
    one.course[0] = "IT";
    one.course[1] = "Interior Design";
    one.course[2] = "Mass Comm";
    one.course[3] = "Business";

    print(one.name, one.history, one.founder);

    cout<<"Our Course: ";

    //problem here

    int numberofelements = sizeof(one.course)/sizeof(one.course[0]);

    for (int i = 0; i < numberofelements; i++){
        if(i == numberofelements-1){
            cout << one.course[i];
        }
        else{
            cout << one.course[i] << ", ";
        }
    }

    // problem here

    return 0;
}

void print(string r, int x, string y){
    cout<<"Our College Name: "<<r<<endl;
    cout<<"Our History: "<<x<<endl;
    cout<<"Our Founder: "<<y<<endl;
};

Or if you want a cleaner way, you can modify your void print method to take an array parameter that gets passed into a for loop in the method body and print the array elements out.

hiew1
  • 1,394
  • 2
  • 15
  • 23
  • thx, but can you explain it in more details, i dont understand the line `sizeof(one.course)/sizeof(one.course[0]);`, i cout them, why is sizeof(one.course)=32 and sizeof(one.course[0])=8 and also the forloop part i dont really understand, can you explain it also? appreciate for your help – Newbie Mar 30 '19 at 12:47
  • Doing the sizeof(one.course) can give you the total number of bytes allocated for the whole array itself, doing sizeof(one.course[0]) can give you the number of bytes one element is allocated, so by dividing the total bytes of all the elements in the array by the number of bytes of 1 element, you can find out how many elements in the array. – hiew1 Mar 30 '19 at 12:52
  • And the number of elements in the array is how many times your for loop should loop through so that all elements can be taken cared of, without going over which will cause an error. – hiew1 Mar 30 '19 at 12:53
  • The for loop part, we use i as the iterator, starts at 0, and then the i++ means the i will keep incrementing by 1 until it is i < numberofelements, where it will stop looping. – hiew1 Mar 30 '19 at 12:55
  • The reason there is an if else statement is because you don't want to add an extra comma at the end of your output. – hiew1 Mar 30 '19 at 12:56
  • thanks a lot, your explanation is clear and helpful!! – Newbie Mar 30 '19 at 14:04