How do I instead of entering a name, enters 'quit' and it will close the program?
string name;
cout << "Enter a name: "<< " ";
std::getline (std::cin,input);
input[0] = toupper (input[0]);
C++ is rusty something like this...
string name;
cout << "Enter a name: "<< " ";
std::getline (std::cin,input);
input[0] = toupper (input[0]);
if (input[0] == 'quit')
{
std::exit;
}
Using Visual C++
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
string strinput;
while (strinput != "quit")
{
cout << "Enter a name: " << endl;
cin.clear();
cin >> strinput;
if(strinput =="quit")
exit(0);
}
return 0;
}
Related
The word "getline" is a red and underlined, which indicate an error:
#include <iostream>
using namespace std;
string ReadName()
{
string Name;
cout << "Please Enter Your Name: " << endl;
getline(cin, Name);
return Name;
}
void PrintName(string Name)
{
cout << "\nYour name is " << Name << endl;
}
int main()
{
PrintName(ReadName());
return 0;
}
The VS Gives an error regarding getline
When runnig my project i see these problems. Does anyone know the solution?
'std array': too few template arguments.
cannot use this indirection on type 'std::array'.
too few arguments for class template "std::array".
I tried to remove using namespace std; into std::array or cli::array but it didn't helped a lot
#include "pch.h"
#include "windows.h"
#include <iostream>
#include <string>
using namespace System;
using namespace std;
class Dog
{
public:
Dog();
Dog(int initAge);
~Dog();
int GetAge();
void SetAge(int Age);
void GawGaw(string S);
private: int itsAge;
};
Dog::Dog()
{ }
Dog::Dog(int initAge)
{
itsAge = initAge;
}
Dog::~Dog()
{ }
int Dog::GetAge()
{ return itsAge;
}
void Dog::SetAge(int Age)
{ itsAge = Age;
}
void Dog::GawGaw(string S)
{ cout << S + " -> GawGaw \n";
}
int main(array<System::String ^> ^args)
{
cout << "\n My Favorite dogs \n";
Dog Bob;
cout << "\nBob is a dog who is ";
cout << Bob.GetAge() << " years old. \n";
Bob.SetAge(7);
cout << "No. Bob is a dog who is ";
cout << Bob.GetAge() << " years old. \n";
Bob.GawGaw("Bob");
Dog* Fox = new Dog(10);
cout << "\nFox is a dog who is ";
cout << Fox->GetAge() << " years old. \n";
Fox->SetAge(12);
cout << "No. Fox is a dog who is ";
cout << Fox->GetAge() << " years old. \n";
Fox->GawGaw("Fox");
Dog Rex(5);
cout << "\nRex is a dog who is ";
cout << Rex.GetAge() << " years old. \n";
Rex.SetAge(8);
cout << "No. Rex is a dog who is ";
cout << Rex.GetAge() << " years old. \n";
Rex.GawGaw("Rex");
Console::ReadLine();
return 0;
}
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");
}
I'm writing class to r/w files from OS file system or from my own archives format in my game engine. How can I make impossible to open file by std::fopen() or std::fstream in modes "rw" and "r". I have written some code to test that on Linux. Here's it:
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
fstream in("file.txt", ios::in | ios::out);
if(!in.is_open())
{
cout << "Plik nie może być otwarty w trybie rw" << endl;
return 1;
}
cout << "Plik otwarty w trybie rw" << endl;
in << ".test.";
cout << "Wpisano tekst" << endl;
while(1){}
return 0;
}
/* Drugi plik */
/* The second src code */
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
fstream in("file.txt", ios::in);
if(!in.is_open())
{
cout << "Plik nie może być otwarty w trybie r" << endl;
return 1;
}
cout << "Plik otwarty w trybie r" << endl;
cout << in << endl;
return 0;
}
When I've executed the ./rw program and some instances of ./r, the ./rw has gone into endless loop and the instances of ./r have terminated with 0 code.
Sorry for my English. :)
You should "lock" the file using lockf(): http://man7.org/linux/man-pages/man3/lockf.3.html
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;
}