How to cin data to store in a struct defined in another header file? - visual-c++

I am working on my homework and am stuck at this place. It would he helpful if someone could help me with this. I am using Visual C++.
This is a header file with a class.
#ifndef CALLER_H
#define CALLER_H
#include <string>
#include <time.h>
#include "nodeData.h"
using namespace std;
class Caller {
private:
int phone_number;
void setPhoneNumber(){
int min = 300000;
int max = 800000;
phone_number = min + rand()%(max-min+1);
}
string callers_name;
string message;
};
#endif
I am trying to store string of name and message into the above header file but I am getting error. I have searched on google and this was one possible solution: "cin >> Caller.callers_name;" but still getting error. It is same for "cin >> Caller.message;".
int _tmain(int argc, _TCHAR* argv[])
{
QueueList L;
Caller * caller = NULL;
int choice;
const int columnSize = 3;
string columnNames[columnSize];
cout <<"\n\t\tANZ Call Center Simulation\n";
cout <<"\n\tPress 1 to make a call by a caller.\n";
cout <<"\tPress 2 to receive a call by a consultant.\n";
cout <<"\tPress 3 to print all callers.\n\tChoice: ";
cin >>choice;
if (choice == 1){
cout <<"\n\tPlease leave your name and message.\n";
cout <<"\tEnter Name: ";
cin >> Caller.callers_name;
cout <<"\n\tMessage: ";
cin >> Caller.message;
}else if (choice == 2){
}
..............
system("pause");
return 0;
}

Related

Microsoft's code is not working in praxis

This code, under msvc 2022:
const char* chars = "XBECEDX";
for each (char c in chars)
{
if (c == 'X') std::cout << 'A';
else
std::cout << c;
}
Ends with error E0125, E0065, E0029
Maybe output is: ABECEDA
Where is the Failure? Is it bad code or bad compiler or compiler setting or any Failure.
I solve this as vector with lambda function. Here is example:
string s = "XBECEDX";
for_each(s.begin(), s.end(), [](char ch)
{
if (c == 'X') std::cout << 'A';
else std::cout << c;
});
Why not this:
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
string chars = "XBECEDX";
// char chars[] = "XBECEDX"; // will also work as an alternate to string
for (char c : chars)
{
if (c == 'X') std::cout << 'A';
else
std::cout << c;
}
return 0;
}

How to print eight words to a line using vector<string>?

I was trying to make a program write 8 words to a line after a user enter their sentence.Its only printing words that have been typed in and i don't have a clue how to make it type 8 words to a line.
#include <iostream>
#include <vector>
#include <string>
#include <cctype>
using namespace std;
vector<string> sentence;
string sente = "";
void print(string, string);
template<typename T>
void print(vector<T>& v, string)
{
cout << "Enter your sentence " << endl;
getline(cin, sente);
sentence.push_back(sente);
for (auto const elem: sentence)
{
cout << elem;
}
}
int main()
{
print(sentence,sente);
}
Using global variables is generally not a good practice.
Also you don't need a extra vector for your use case.
Take a look at the following code, where you can smartly make use of istringstream for your use case:
#include <iostream>
#include <string>
#include <sstream>
void print()
{
std::string sente;
std::cout << "Enter your sentence " << std::endl;
getline(std::cin, sente);
// Used to split string around spaces.
std::istringstream ss(sente);
int wordCountPerLine = 0;
int requiredWordsPerLine = 8;
// Traverse through all words
do {
// Read a word
std::string word;
ss >> word;
// Print the read word
std::cout << word << " ";
wordCountPerLine++;
if(wordCountPerLine % requiredWordsPerLine == 0){
std::cout<<std::endl;
wordCountPerLine = 0;
}
// While there is more to read
} while (ss);
}
int main()
{
print();
}
Feel free to ask any doubts.

ability to run a packaged task with std::bind function parameters in a seperate thread via template function

The question i have is the line where i indicate ERROR below gives error as std::thread constructor works out the function to be invoked and requires
the parameter as needed by the function signature.
Is there any way to solve this ? if i attempt to decode the function name and argument list from packaged_task then i cant use the get_future function over packaged task and need to add my own promise/future code to handle this.
#include<iostream>
#include<string>
#include<thread>
#include<future>
#include<functional>
using namespace std;
int sampleAddFunction(const int& a, const int& b)
{
int sum = a + b;
cout << "sum = " << sum << endl;
return(sum);
}
template<typename T> T asyncExecutor(std::packaged_task<T(T, T)>&& package)
{
std::future<T> result = package.get_future();
std::thread task_td(std::move(package)); // ERROR here as the std::thread identifies the function name from package and requires the params to be passed. How to handle this ?
task_td.join();
return(result.get());
}
int main(int argc, char* argv[])
{
// Executing via directly calling through main.
int testResult1 = sampleAddFunction(100, 200);
cout << "testResult1 = " << testResult1 << endl;
// Attempt to create a std::packaged_task and then run it in another thread.
std::packaged_task<int(int,int)> task(std::bind(sampleAddFunction, 10, 20));
std::future<int> result = task.get_future();
std::thread t(std::move(task), 100, 200); // 100 and 200 are dummy parameters.
t.join();
int testResult2=result.get();
cout << "testResult2 = " << testResult2 << endl;
// Attempt to run this in seperate thread and get results.
std::packaged_task<int(int,int)> task2(std::bind(sampleAddFunction, 15, 27));
int testResult3 = asyncExecutor<int>(std::move(task2), 100, 200);
cout << "testResult3 = " << testResult3 << endl;
}
This should work.
#include<iostream>
#include<string>
#include<thread>
#include<future>
#include<functional>
using namespace std;
int sampleAddFunction(int a, int b)
{
int sum = a + b;
cout << "sum = " << sum << endl;
return(sum);
}
template<typename R, typename F, typename... Ts>
R asyncExecutor(F&& package, Ts... args)
{
std::future<R> result = package.get_future();
std::thread task_td(std::move(package), args...);
task_td.join();
return(result.get());
}
int main(int argc, char* argv[])
{
std::packaged_task<int(int,int)> task2(sampleAddFunction);
int testResult3 = asyncExecutor<int>(std::move(task2), 15, 27);
cout << "testResult3 = " << testResult3 << endl;
}
You are constructing a binary packaged_task (std::packaged_task<int(int,int)>) from a nullary function, the result of your bind (std::function<int()>).
You should either not use bind, or have asyncExecutor accept a nullary packaged_task (std::packaged_task<T()>)
int sampleAddFunction(const int& a, const int& b)
{
int sum = a + b;
cout << "sum = " << sum << endl;
return(sum);
}
template<typename T, typename ... ARGS> T asyncExecutor(std::packaged_task<T(ARGS...)>&& package, ARGS ... args)
{
std::future<T> result = package.get_future();
std::thread task_td(std::move(package), args...);
task_td.join();
return(result.get());
}
int main(int argc, char* argv[])
{
std::packaged_task<int()> task(std::bind(sampleAddFunction, 10, 20));
int testResult = asyncExecutor(std::move(task));
cout << "testResult = " << testResult << endl;
std::packaged_task<int(int,int)> task2(sampleAddFunction);
int testResult2 = asyncExecutor(std::move(task2), 15, 27);
cout << "testResult2 = " << testResult2 << endl;
}

Can't initialize float array with {0}

My Compiler gives runtime error when I initialize the float array in Veccreator function. I am here posting just a sample of what my code looks like.
#include<iostream>
using namespace std;
#define SIZE 1000
class Vector
{
private:
float vecarray[SIZE];
public:
void VecCreator(int dimension)
{
vecarray[SIZE]= { 0 };
cout << "Enter " << dimension << " digits" << endl;
for (int i = 0; i < dimension; i++)
{
cin >> vecarray[i];
}
}
};
int main(void) {
Vector obh;
obh.VecCreator(2);
}
But it works fine with this:`
#include<iostream>
using namespace std;
#define SIZE 1000
class Vector
{
private:
float vecarray[SIZE]= {0};
public:
void VecCreator(int dimension)
{
cout << "Enter " << dimension << " digits" << endl;
for (int i = 0; i < dimension; i++)
{
cin >> vecarray[i];
}
}
};
int main(void) {
Vector obh;
obh.VecCreator(2);
}
Please tell me why the first code is giving error.
Look at the second answer here:
https://social.msdn.microsoft.com/Forums/vstudio/en-US/14e7318e-6fff-4d68-a823-9cbe7b7bc20a/debugging-runtime-check-failure-2-stack-around-the-variable-loggerthread-was-corrupted?forum=vcgeneral
why not do it like here below? i mean if you want to put values in there, why initially put 0 in there?
private:
float vecarray[SIZE];
public:
void VecCreator(int dimension)
{
cout << "Enter " << dimension << " digits" << endl;
for (int i = 0; i < dimension; i++)
{
cin >> vecarray[i];
}
}

Looping back again to Start

now I'm Having problem in repeating the loop after it finished doing the first and i want to try it again without exiting the program? I've been using while loop to do it but still no joy. so i decided to do the if statement. But the Array only accept 4 strings then it exit. Any one who can help? TIA.
#include "stdafx.h"
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
template <typename T>
void GetContents(T& Input);
template <typename T>
void DisplayContents(const T& Input);
int main()
{
int PASS = 0;
// To Display the unsorted and sorted Book Titles
std::vector<std::string> books;
GetContents(books);
std::cout << "\nMy original library (number of books: " << books.size() << "):\n\n";
DisplayContents(books);
std::sort(books.begin(), books.end());
std::cout << "\nMy sorted library (number of books: " << books.size() << "):\n\n";
DisplayContents(books);
std::cout << "Press 1 to try again, else to quit: ";
std::cin >> PASS;
std::cout << "\n";
if (PASS == 1)
{
GetContents(books);
}
else
{
return 0;
}
// to input All book titles
template <typename T>
void GetContents(T& Input)
{
const int MAX = 5;
string bookName;
std::cout << "Enter a Book Titles:\n> ";
for (int i = 0; i < MAX; i++)
{
std::getline(std::cin, bookName);
Input.push_back(bookName);
std::cout <<">";
}
}
//Display All input book titles
template <typename T>
void DisplayContents(const T& Input)
{
for (auto iElement : Input)
{
std::cout << iElement << '\n';
}
std::cout << '\n';
system("pause");
}

Resources