cin.get() broke after adding stat() check - c++

So I wanted to add a stat() check on a dir. After compiling it started tossing some really strange stuff. Heres what I got:
#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <boost/algorithm/string.hpp>
#include <sys/stat.h>
using namespace std;
using namespace boost;
struct stat sb;
int main()
{
cout<<" \n";
cout<<" Account Progam\n"
cout<<" \n";
cout<<" Created by: Illyduss\n";
cout<<" 2015\n";
cout<<" \n";
cout<<" \n";
cout<<" \n";
std::string account;
cout<<"Do you already have an account? \n";
cout<<"Type 'NEW' to create an account or enter your account name.\n";
cout<<"Account Name: ";
cin>> account;
cin.ignore();
string str1(account);
to_upper(account);
if (account == "NEW"){
...Some if checking here and junk...
...And then what was recently added the bugs my cin.get()...
else {
cout<< "Welcome back, '" << account << "'!\n";
struct stat sb;
if (stat("/home/user/Account Program/accounts",&sb) == 0 && S_ISDIR(sb.st_mode)){
cout<<"Please enter your password: ";
}
else {
cout<< "There is no account by that name, please type NEW to create a new account.\n";
}
}
}
cin.get();
}
I added cin.get() to keep the terminal open after it's run so I can see whats going on, but I believe it is conflicting with my dir check because it was running just fine until I added that check in to see if an account was already made before attempting to open the account dir.
Specifically I am getting these errors:
intro.cpp:112:2: error: ‘cin’ does not name a type
cin.get();
^
intro.cpp:113:1: error: expected declaration before ‘}’ token
}
^
user#computer:~/Account Program$ g++ intro.cpp
intro.cpp:112:2: error: ‘cin’ does not name a type
cin.get();
^
intro.cpp:113:1: error: expected declaration before ‘}’ token
}
^
As well it is tossing me an error on the } used to close out int main() like once it gets to the dir check it tosses me out of my int main()? Am I not closing off the stat() correctly?

The answer is:
#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <boost/algorithm/string.hpp>
#include <sys/stat.h>
using namespace std;
using namespace boost;
struct stat sb;
int main()
{
cout<<" \n";
cout<<" Account Progam\n"
cout<<" \n";
cout<<" Created by: Illyduss\n";
cout<<" 2015\n";
cout<<" \n";
cout<<" \n";
cout<<" \n";
std::string account;
cout<<"Do you already have an account? \n";
cout<<"Type 'NEW' to create an account or enter your account name.\n";
cout<<"Account Name: ";
cin>> account;
cin.ignore();
string str1(account);
to_upper(account);
if (account == "NEW"){
...Some if checking here and junk...
...And then what was recently added the bugs my cin.get()...
else {
cout<< "Welcome back, '" << account << "'!\n";
struct stat sb;
if (stat(("/home/user/Account Program/accounts/"+account).c_str(),&sb) == 0 && S_ISDIR(sb.st_mode)){
cout<<"Please enter your password: ";
}
else {
cout<< "There is no account by that name, please type NEW to create a new account.\n";
}
}
cin.get();
}
I had an extra } after the last else and needed to add the child dir that was being checked via like so ("/home/user/Account Program/accounts/"+account).c_str().

Related

Creating a string named file in a specific directory

I just got into programming and was doing a fun little exercise in c++ ( a login app using .dat's as a database ) and ran into a problem. Is there any way to create a "string named file" in a specific directory? I've seen that ones can create both a string named file and a file in a specific directory but cant seem to find the answer for this question. Once again sorry if this has already been gone over and thank you.
This is the part I was on about I'll put the full code down below to get some more opinions as well, just do take in mind that I'm a beginner and the code isn't finished.
int DataBase() {
ofstream DBName(( /*Potencially my directory? + */ BaseAccount + ".dat").c_str());
DBName << Pass << endl;
}
My full code will be down here, im fully open to new suggestions, yet, I do already have some parts of the code that I want to keep them as it is, again do remember that I'm a starter and thank you for your attention!
#include <iostream>
#include <time.h>
#include <stdlib.h>
#include <ctype.h>
#include <fstream>
#include <string>
using namespace std;
int UserVar;
string Account, Pass, AccountLog;
int main(), login();
int DataBase() {
ofstream DBName(( /*Potencially my directory? + */ BaseAccount + ".dat").c_str());
DBName << Pass << endl;
}
void wait(int seconds){
clock_t endwait;
endwait = clock () + seconds * CLOCKS_PER_SEC ;
while (clock() < endwait) {}
}
void DataBaseCheck(string AccCheck){
ifstream DBLookForAcc(AccCheck.c_str());
cout<<"Looking for the user in the data base..."<<endl;
wait(2);
system("cls");
string AccData, AccPass, DBPass;
int DBVarTries=3;
if (DBLookForAcc){
cout<<"Account Found!"<<endl;
wait(1);
system("cls");
ifstream AccData;
AccData.open(AccCheck);
AccData>>DBPass;
do{
cout<<"Please enter your password: ";
cin>>AccPass;
if(AccPass==DBPass){
system("cls");
cout<<"You sucessfully logged in!"<<endl;
exit(0);
}
else (AccPass==DBPass);{
DBVarTries=DBVarTries-1;
system("cls");
cout<<"The password is wrong, please try again!"<<endl<<endl<<DBVarTries<<" attempts remaining. ";
wait(2);
system("cls");
continue;
}
}
while(DBVarTries!=0);
}
else {
system("cls");
cout<<"Sorry... We couldnt find your account."<<endl;
wait(1);
login();
}
}
int login(){
system("cls");
cout<<"Sign in"<<endl<<endl<<endl<<"User Name: " ;
cin>>AccountLog;
system("cls");
DataBaseCheck((AccountLog + ".dat").c_str());
}
int main(){
do{
system("cls");
cout<<"Sign up"<<endl<<endl<<endl<<"Insert your user: ";
cin>>Account;
if(Account=="1"){
do{
login();
}
while(true);
}
cout<<"Insert your accounts password: ";
cin>>Pass;
if(Pass=="1"){
do{
login();
}
while(true);
}
system("cls");
DataBase();
cout<<"The account was saved to the Data Base. ";
wait(2);
system("cls");
UserVar=UserVar+1;
}
while(UserVar=4);
cout<<"You cant sign any more users up."<<endl<<endl<< "Login protocol" ;
login();
}

Simple Input validation - cin.ignore (error)

I have this problem while watching tutorials and make one for myself.
I can't get a hold of this cin.ignore(). I want the user to type characters only but if he types any integer, it should continue looping until he gets the correct input.
This is my code along with the error mentioned.
#include <iostream>
#include <vector>
#include <climits>
using namespace std;
// simple io validation
int main(){
char name{};
char last{};
int birthday {};
cout << "Pls Enter your first name: ";
cin>>name; // if he type "alfred123 Philip" it should error
while (!cin.good()) // or while cin.fail()
{
cout<<"Please enter character only"<<endl;
cin.clear();
cin.ignore(INT_MAX,"/n"); //error no instance of overloaded function "std::basic_istream<_CharT, _Traits>::ignore [with _CharT=char, _Traits=std::char_traits<char>]" matches the argument list
cout<<"Enter Name: "; // ask again the user
cin >> name;
}
cout << endl;
cout<<"Hello "<<name<<endl;
return 0;
}

My code to change the current directory in C++ keeps giving me the Segmentation Fault: 11 error

Here is the code:
#include <iostream>
#include "unistd.h"
using namespace std;
int main(){
char *directory = NULL;
cout << "Enter the directory you want to enter: ";
//taking input
cin >> directory;
//changing the directory
chdir(directory);
return 0;
}
My compiler says that the error occurs at line 7 (char *directory = NULL;)
Any help with this would be appreciated.
It's got nothing to do with chdir(), rather the problem is that you are telling cin to write data via a NULL pointer.
The proper solution is to use a std::string to hold the path-string instead:
#include <string>
#include <iostream>
#include <unistd.h>
int main()
{
std::string directory;
std::cout << "Enter the directory you want to enter: " << std::endl;
//taking input
std::cin >> directory;
//changing the directory
chdir(directory.c_str());
return 0;
}
use <> for file in compiler library
#include <iostream>
#include <unistd.h> //
using namespace std;
int main()
{
string directory;
cout << "Enter the directory you want to enter: " << endl;
//taking input
cin >> directory;
//changing the directory
chdir(&directory[0]);
return 0;
}

std::ifstream::open error on eclipse

I'm trying to write a program that gives a table of temperature for each hour in one day with some starts that give an idea about the temperature. My problem is that eclipse (I use it on Mac OS) gives me "method 'open' can not be resolved". Would you please help me solving this problem?
And it would be so great if you have any suggestions in order to better the program.
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <cstdlib>
#include<cstring>
using namespace std;
int main()
{char temp[30];
cout<<"what is the name of the file you are using? ";
int rain[24],i,j,stars;
cin>>temp;
ifstream input;
input.open(temp);
if(input.fail())
{ cout<<"file did not open please check it\n";
system("pause");
return 1;
}
for(i=0;i<24;i++)
{input>>rain[i] ;
}
for(i=-30;i<=120;i+=30)
cout<<setw(10)<<right<<i;
cout<<endl;
for(i=0;i<24;i++)
{cout<<setw(10)<<left<<rain[i];
if(rain[i]<0)
{stars=(int)(abs(rain[i]) /3.);
for(j=1;j<10-stars;j++)
cout<<" ";
for(j=0;j< stars;j++)
cout<<"*";
cout<<"|\n";
}
else
{for(j=1;j<10;j++)
cout<<" ";
cout<<"|";
stars=(int)(rain[i]/3.);
for(j=0;j<stars;j++)
cout<<"*";
cout<<endl;
}
}
input.close();
system("pause");
return 0;
}
This should work..
Open Project explorer, right click on your project > Index > Rebuild

Why is my program behaving oddly

I have just started work on asmall program that sends text over net (TON) I havent quite started but when I compile this happens when I have a space in my name
Username: Knight Hawk3
LAN (1) or Net (0):
(Here it wont let me enter data)
Process returned 1 (0x1) execution time : 3.670 s
Press any key to continue.
Here is what normally (should) happen
Username: Knight
LAN (1) or Net (0):
1
Process returned 1 (0x1) execution time : 2.134 s
Press any key to continue.
Here is my source
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Network.hpp>
#include <iostream>
#include <string>
using namespace std;
bool firstrun = true;
int main() {
if (firstrun) {
cout << "Username: ";
string name = "";
cin >> name;
while (name == "") {
cout << "Invalid Enter a new name: ";
cin >> name;
cout << "\n";
}
}
cout << "LAN (1) or Net (0): ";
int type;
cout << "\n";
cin >> type;
while (type < 0) {
cout << "Invalid LAN (1) or Net (0): ";
cin >> type;
}
return true;
}
I am running WIN7, Using Code::Blocks
Why is this happening with a space?
std::cin reads till the first space but keeps the rest in buffer, which will be used on the following std::cins.
If you want to read till the first '\n'(hitting enter) you have to replace std::cin with
std::getline(std::cin, name);