vendredi 1 avril 2016

c++ How to display the info from a certain function in the main()

How do I have the display function display the info within its function body? How to call it? The function is filled with the data I want outputted yet how do I call it and display the contents of the function.

int main()
{
    InputValue();
    displayinfo();




}

void displayinfo(int d, array<char, 7> b)
{
    for (int k = 0; k < d; k++)
    {
        cout << "Semester: " << b[k] << endl;
    }

}

Partial class template specialization c++11

I am trying to compile the below code, but I am getting the error:

wrong number of template arguments

template<int start, int end, int step>
struct range{};

template<int start, int end>
struct range<start, end, 1>{};

template<int end>
struct range<0, end, 1>{};

int main() {
    auto r1 = range<0, 5, 2>{};
    auto r2 = range<5, 15>{}; //error: wrong number of template arguments
    auto r3 = range<10>{}; //error: wrong number of template arguments
}

How can I create partial template class object?

Is length of std::string consistent despite string having null terminated characters?

I've got the following piece of code:

std::string header(4, char{});
assert(header.length() == 4);

I'm wondering whether my assertion shall hold valid for the most popular compilers (VisualC++, GCC, clang). If not, for which versions does it hold valid, or from which version onward does it hold valid.

I'm assuming it must hold valid, and my assumption is from this reference:

My question also arises because this is inconsistent with the traditional behavior of strlen, and char_traits::length()

C++ How to pass arrays to function then have it called to display the values of the arrays?

Hello everyone Im having a very hard problem doing what I need this code to do for my c++ assignment!

The desire I want my code to do is have the function InputValue() fill up the arrays I have made; Which it does perfectly! Then pass those arrays to a function called displayinfo() which will display the contents of the array when called in the main() function!

The problem I have ran into is when I try to call on displayinfo(sem1array) with the array I want to pass ultimately all of them, it comes up with a syntax error!

How should I pass the contents of the various arrays sem1array,sem1coursetypes, sem1coursetitles,... into the function that displays the contents and then how should I call on the function to display the contents within main() function?

And another huge question I have is; Is there another way of writing or declaring an array other than array (right open arrow)char,7(left open arrow) sem1array; like the way I have declared all the arrays in my code?

I have put stars on all the parts where i'm having the problem! I left the other code in so you see how the arrays are filled and what i'm working with.

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

void setCourseName(string);
void InputValue();


int main()
{
    InputValue();

    ***displayinfo();***



}

void InputValue()
{
    char semester;
    string courseTitle;
    string courseType;
    int credits;
    string lettergrade;
    double gpa;

    array<char, 7> sem1array;
    array<string, 7> sem1coursetitles;
    array<string, 7> sem1courseTypes;
    array<int, 7> sem1credits;
    array<string, 7> sem1lettergrades;


    for (int counter = 0; counter < sem1array.size(); counter++)
    {
        bool valid1 = false;
        bool validCredits = false;
        bool valid3 = false;
        bool valid4 = false;
        while (valid1 == false)
        {
            cout << "Enter Semester Number" << endl;
            cin >> semester;
            if (semester == '1' || semester == '2')
            {
                cout << "valid input" << endl;
                sem1array[counter] = semester;
                valid1 = true;
            }
            else
            {

                cout << "Invalid Input! RE ENTER SEMESTER NUMBER EITHER 1 OR 2!" << endl;
            }
        }
            cout << "Enter Course Title: " << endl;
            cin.ignore();
            getline(cin, courseTitle);
            if(courseTitle.size() <= 25)
                sem1coursetitles[counter] = courseTitle;
            else
            {
                cout << "Course Name Cannot Be More Than 25 Characters; Course name limited to first 25 characters!" << endl;
                courseTitle = courseTitle.substr(0, 25);
                sem1coursetitles[counter] = courseTitle;
            }

        while (valid3 == false)
        {
            cout << "Enter Course Type: Regular, AP, or Honors" << endl;

            getline(cin, courseType);
            if (courseType == "Regular" || courseType == "AP" || courseType == "Honors")
            {
                sem1courseTypes[counter] = courseType;
                valid3 = true;
            }
            else
            {
                cout << "Invalid Input! RE ENTER COURSE TYPE EXACTLY HOW IT APPEARS EITHER Regular, AP, or Honors!" << endl;
            }
        }
        while(validCredits == false)
        {
            cout << "Enter Credits Earned For Course: **Can Either Be 1-4 Credits**" << endl;
            cin >> credits;
            if (credits == 1 || credits == 2 || credits == 3 || credits == 4)
            {
                sem1credits[counter] = credits;
                validCredits = true;
            }
            else
            {
                cout << "Invalid Output! Must Enter Number 1-4 For Credits Earned!" << endl;
            }
        }
        while (valid4 == false)
        {
            cout << "Enter Letter Grade; Capital Letter Followed By A Plus + or Minus - If There Is One!Example: A+, A-, A, B+..." << endl;
            cin.ignore();
            getline(cin, lettergrade);
            if (lettergrade == "A+")
            {
                gpa = 4.0;
                sem1lettergrades[counter] = lettergrade;
                valid4 = true;
            }
            else if (lettergrade == "A")
            {
                gpa = 4.0;
                sem1lettergrades[counter] = lettergrade;
                valid4 = true;
            }
            else if (lettergrade == "A-")
            {
                gpa = 3.7;
                sem1lettergrades[counter] = lettergrade;
                valid4 = true;
            }
            else if (lettergrade == "B+")
            {
                gpa = 3.3;
                sem1lettergrades[counter] = lettergrade;
                valid4 = true;
            }
            else if (lettergrade == "B")
            {
                gpa = 3.0;
                sem1lettergrades[counter] = lettergrade;
                valid4 = true;
            }
            else if (lettergrade == "B-")
            {
                gpa = 2.7;
                sem1lettergrades[counter] = lettergrade;
                valid4 = true;
            }
            else if (lettergrade == "C+")
            {
                gpa = 2.3;
                sem1lettergrades[counter] = lettergrade;
                valid4 = true;
            }
            else if (lettergrade == "C")
            {
                gpa = 2.0;
                sem1lettergrades[counter] = lettergrade;
                valid4 = true;
            }
            else if (lettergrade == "C-")
            {
                gpa = 1.7;
                sem1lettergrades[counter] = lettergrade;
                valid4 = true;
            }
            else if (lettergrade == "D+")
            {
                gpa = 1.3;
                sem1lettergrades[counter] = lettergrade;
                valid4 = true;
            }
            else if (lettergrade == "D")
            {
                gpa = 1.0;
                sem1lettergrades[counter] = lettergrade;
                valid4 = true;
            }
            else if (lettergrade == "F")
            {
                gpa = 0.0;
                sem1lettergrades[counter] = lettergrade;
                valid4 = true;
            }

            else
            {
                cout << "Invalid Input! Please Re-Enter Letter Grade! Example: A+, A-, A, B+..." << endl;
            }
        }
    }

    ***displayinfo(sem1array);***

}
***void displayinfo(char b[])***
{
    for (int k = 0; k < ***sem1array.size()***; k++)
    {
        cout << "Semester: " << sem1array[k] << "           " << "Course: " << sem1coursetitles[k] << endl;

    }

C++ using standard library synchronization tools with the C++ REST SDK

When using the asynchronous tasks from the C++ REST SDK, is it possible to apply synchronization tools from the multithreading standard library? Example:

http_client client(U("whatever"));
http_request request;

int i = 0;
client.request(request).then([&i](http_response response) { i = 1;});
//here use variable i

The above code obviously introduces a race condition, which in principle could be cured by using a std::mutex or std::atomic. Is this possible, i.e. does the underlying asynchronous task implementation respect this mutex? And if it is possible, is it so in a platform-independent way?

C++ - Getting Derived type as the template parameter

I've got a little templating scenario I'm dealing with. Here's the setup for it.

class Base {

}

class Derived : Base {

};

And my use case:

void DoAThing(Base* t) {
     DoAnotherThing(*t);
}

template<typename T>
void DoAnotherThing(T& t) {
}

So what I was hoping for was that I would be able to pass a Derived type into my "DoAThing", and then when I pass it through to DoAnotherThing, the template parameter would appear as Derived. I can see that the dereferenced type definitely is resolved by typeid as Derived, but the T type of DoAnotherThing always comes through as Base. Is there any way I can manipulate this so that DoAnotherThing resolves to the derived type that I send?

Edit: Thanks for the answers so far, but it's not quite solved for my use case yet. DoAnotherThing is to actually to create a container of the Derived class, and the problem is that the container type ends up as Container rather than Container. I want DoAThing to continue taking in the Base rather than be templates for the scenarios in which it is called.

Understanding what && does [duplicate]

This question already has an answer here:

If I have

int x = 25;
int& y = x;

I understand that x and y point to the same memory location. However, when I have:

int x = 25;
int&& y = x;

I get:

error: expected unqualified-id before '&&' token

What is the explanation in terms of lvalue and rvalue? I cannot wrap my head around the concept and how it is applied here.

Also, is it true that if you want to reference a number, you should only be able to do it using something like const int& z = 25 and never int& z = 25 because of the reason that you are referencing a number and numbers don't change?