So i'm trying to make a simple console application that asks the user for certain characteristics. The first question, asks the user for their age. For example, it should look like "I am >enter age< years old" I've been having a lot of console application problems, so I'll probably be moving to GUI interfaces in the future.. Until then, I think this is good practice. Heres my code. (Sorry for not using the code format, it doesnt seem to work properly on my mobile)
#include <iostream>
#include <string>
using namespace std;
//Variables
int Age;
//Functions
int AgeEnt(){
cin >> Age;
return Age;
}
//Main
int main (){
cout << "Welcome! Please enter your age to continue\n";
cout << "I am " << AgeEnt << " years old";
return 0;
}
This automatically puts a 1 where the age should be. How would I make it to where I can input a number between the text? Im still a beginner so excuse me if this isnt possible in console, or extremely depreciated.
int main (){
cout << "Welcome! Please enter your age to continue\n";
int age;
cin >> age;
return 0;
}
Also, why do you declare functions and variables above your main? Except for global functions, this is not needed in C++
#include <iostream>
using namespace std;
//Functions
int AgeEnt(){
int Age;
cin >> Age;
return Age;
}
//Main
int main (){
cout << "Welcome! Please enter your age to continue\n";
AgeEnt();
cout << "I am " << AgeEnt() << " years old";
return 0;
}
A few things
You don't need the <string> header file for this. Its not being used anywhere.
You don't need to declare Age as a global variable. Its only being used in one function so declare it in that.
Also, although i haven't done it here, i would usually do something like int a = AgeEnt(); cout<<a;
Related
I just started learning C++ and I'm currently following a tutorial on YouTube.
I thought it was fun to make a very simple 'access' program. If I type in my name it says, "Welcome!" If I type in another name it says, "access denied". It worked perfectly fine, but then I wanted the program to say "Welcome!" to two different names. So, I wanted to add a second name in the string, but I couldn't figure out how to do that. I googled a lot but I couldn't find anything. In the end, I came to string name = ("Joe", "Sean");, but here, it was only valid for Sean. I just can't figure out how to put multiple names in one string and make them both work. I hope you can help me, here is my code:
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
int main()
{
string name = ("Joe", "Sean");
string input;
cout << "What is your name?\nMy name is: ";
cin >> input;
if(input == name){
cout << "Welcome, "<< input <<"! ";
} else {
cout << "Access denied";
}
return 0;
}
This is a way to do it using a vector of strings, so you can adapt easily with more names :
#include <iostream>
#include <vector>
using namespace std;
void printMessage(string message)
{
std::cout << message << std::endl;
}
int main()
{
vector<string> names{"Joe", "Sean", "Paul"};
string input;
cout << "What is your name? " << endl;
cin >> input;
for (string name : names)
{
if (name == input)
{
printMessage("Welcome!");
return 0;
}
}
printMessage("Access Denied!");
return 0;
}
The problem is in the string variable "name". You need an array of strings, not a single string.
This is an example implementation:
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
int main()
{
string names[] = {"Joe", "Sean"};
string input;
cout << "What is your name?\nMy name is: ";
cin >> input;
for (int i = 0; i < end(names) - begin(names); i++) {
if(input == names[i]){
cout << "Welcome, "<< input <<"! " << endl;
return 0;
}
}
cout << "Access denied" << endl;
return 0;
}
You encountered some quirky features of C++ in the approach you are using to initialize your string variable:
string s1 = ("Joe"); // creates a string "Joe"
string s2 = ("Joe", "Sean"); // creates 2 strings, "Joe" and "Sean", and the variable s2 stores only the latter!
For more details on the different methods for initializing variables there has been an interesting discussion in this previous question.
I've read that getchar() should actually be preferred over _getch() ( _getch() also needs including conio.h ). However, if I use getchar() in this sample program...
#include <iostream>
#include <conio.h>
using namespace std;
class student {
int id;
int marks;
public:
void getdata() {
cout << "\n Enter id";
cin >> id;
cout << "\n Enter marks";
cin >> marks;
}
void putdata() {
cout << "\n" << id << "\t" << marks;
}
};
int main() {
student tom;
tom.getdata();
tom.putdata();
getchar(); // vs. _getch();
return 0;
}
..then using getchar() won't wait for the input of a character to prevent the console window from being closed too early, which _getch() does.
Does anyone know the reason for this? And should getchar() really be preferred over _getch()?
BTW, I'm using MS Visual Studio 2015.
Thanks in advance and kind regards
EDIT: I'd consider my question as not being a duplicate actually, since I was wondering about the reason for the different behavior, which had not been answered under "Is there a decent wait function in C++?", and which got clarified now.
I've been working on this project for my C++ class in which I have to get a collection of information from the user, including the users birthyear and the current year (we have yet to learn how to access the computer's date so it has to be manually retrieved). I'm still fairly early in the process and I've run into this obstacle that I can't seem to work around.
While I've used this process to easily get the name system to work using the custom class, I can't get it to work for the year value. I can only assume the issue is that the year is an int rather than a string, but I can't possibly figure out any other way to get this to work. Could someone please look at this code and help me figure out what the issue is?
Main class:
#include <iostream>
#include <string>
#include "Heartrates.h"
using namespace std;
int main() {
Heartrates myHeartrate;
cout << "Please enter your name (First and Last): ";
string yourName;
getline (cin, yourName);
myHeartrate.setName(yourName);
cout << "\nPlease enter the current year: ";
int currentYear;
getline (cin, currentYear);
myHeartrate.setCyear(currentYear);
cout << "\nYou entered " << currentYear;
}
Heartrate class:
#include <string> //enables string use
class Heartrates {
public:
void setName(std::string yourName) {
name = yourName;
}
std::string getName() const {
return name;
}
void setCyear(int currentYear) {
Cyear = currentYear;
}
int getCyear() const {
return Cyear;
}
private:
std::string name;
int Cyear{ 0 };
};
I keep running into an error that states there is no matching overload function found, yet as you can see I'm using the same structure between both the main class and the header and the name worked just fine.
The version of std::getline you are trying to use there does not accept an int as a argument. See the function marked 2 (C++11) for the function you are trying to call here. std::istringstream can be included from sstream. I would add a std::endl (or new line) to the final print out to make it appear nicer as well.
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main() {
Heartrates myHeartrate;
cout << "Please enter your name (First and Last): ";
// read line
string line;
getline (cin, line);
// line is yourName
myHeartrate.setName(line);
// read line, read int from line
cout << "\nPlease enter the current year: ";
int currentYear;
getline (cin, line);
std::istringstream ss(line);
ss >> currentYear;
myHeartrate.setCyear(currentYear);
cout << "\nYou entered " << currentYear << endl;
return 0;
}
#include <iostream>
#include "stdafx.h"
#include <string>
using namespace std;
int main()
{
//Variables in which student info is stored
string name = "";
//asks user for their name
cout << "/nHello, my name is Hal, what is your name? ";
cin >> name;
cout << "/nWelcome to C++ ";
cout << name;
cout << ", let the adventure begin!";
return 0;
}
Really basic code that I can't seem to get to work. Everywhere I have cin or cout the compiler says they're an undeclared identifier. I've looked at all of the answers on here and other forums and it none of the solutions seem to fix it. Thanks in advance.
Hope you have the "stdafx.h" file that you are trying to include in the right path. The code works fine without the file being included.
I'm having an issue with my simple Hello World code.
I'm not able to cin my name. It says no defined operator ">>" Can someone help please. Below is my code.
#include <iostream>
using namespace std;
int main()
{
int x;
string name;
cout<< "enter name:";
cin>> name;
cout<< "Hello "<< name <<endl;
system("Pause");
}
You have to
#include <string>.