#include < cstdio>
#include < cstdlib>
#include < iostream>
#include < string>
using namespace std;
int main ( int nNumberofArgs, char* pszArgs[])
{
string name;
string password;
int ans1;
printf("Turn on the BEDrive??\n(write an ODD number for 'no' and EVEN 'yes'\n");
cin >> ans1;
while(ans1%2==0)
{
printf("Hello Master... Welcome to the BEDrive System...\nwould you like to login...\n);
cin.ignore(10, '\n');
cin.get();
printf("Enter your login ID: ");
cin >> name;
printf("Enter your password: ");
cin >> password;
if(name = "smartyguy1" && password = "Bhanuhanu1")
{
printf("WELCOME MASTER!!!!.\nDo you want to change your password(y/n)?");
}
else
{
printf("Login FAILED!!!:( ");
}
return 0;
}
}
when I compile this code, it shows error: no match for 'operator&&' (operand types are 'const char [11]' and 'std::__cxx11::string {aka std::__cxx11::basic_string}') plz help me with this
You had some errors in your code. I fixed it for you.
You missed an ending " on line 30.
#include <cstdio> // "< cstdio>" is not correct, no space is allowed here
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
int main ( int nNumberofArgs, char* pszArgs[])
{
string name;
string password;
int ans1;
printf("Turn on the BEDrive??\n(write an ODD number for 'no' and EVEN 'yes'\n");
cin >> ans1;
while(ans1%2==0)
{
printf("Hello Master... Welcome to the BEDrive System...\nwould you like to login...\n"); // you missed ending " here
cin.ignore(10, '\n');
cin.get();
printf("Enter your login ID: ");
cin >> name;
printf("Enter your password: ");
cin >> password;
if(name == "smartyguy1" && password == "Bhanuhanu1")
{
printf("WELCOME MASTER!!!!.\nDo you want to change your password(y/n)?");
}
else
{
printf("Login FAILED!!!:( ");
}
return 0;
}
}
Related
hi guys so we have a school activity, and we are instructed to make an inventory system. I managed to do the input part of the code however i cannot display the same way the picture below shows:
This is the code of my work:
#include <iostream>
#include <iomanip>
#include <cstring>
#include <windows.h>
#include <cstdlib>
#include <fstream>
using namespace std;
//global declaration
int i;
int j;
const int passctr=3;
struct inventory{
double price;
int stock;
int sold;
int left;
};
struct itemType {
inventory items;
string itemname;
string brand;
};
struct userType{
string name;
string password;
};
int main()
{
//declaration
int prodqty,brandqty;
int prodarr;
int qty;
itemType item[prodqty],display;
userType user;
for(i=0;i<passctr;i++)
{
cout<<"Try no."<<i+1<<endl;
string xpassword="pogisibry";
cout<<"PRODUCT INVENTORY SYSTEM"<<endl;
cout<<"Enter Username: ";
getline(cin,user.name);
cout<<"Enter Password: ";
getline(cin,user.password);
try
{
if(user.password!=xpassword)
{
throw user.password;
}
else
{
cout<<"Welcome user "<<user.name<<"!!!"<<endl;
break;
}
}
catch (string reenter)
{
cout<<"Wrong password!"<<endl
<<"Please re enter!"<<endl;
}
system("pause");
system("cls");
}
if (i==passctr)
{
cout<<"locking inventory system!"<<endl
<<"exiting the program...";
exit(1);
}
cout<<"Enter desired number of products: ";
cin>>prodqty;
for(i=0;i<prodqty;i++)
{
cin.ignore();
cout<<"Product ["<<i+1<<"]: ";
getline(cin,item[i].itemname);
cout<<"How many "<<item[i].itemname<<"? : ";
cin>>brandqty;
cout<<endl;
for(j=0;j<brandqty;j++)
{
cin.ignore();
cout<<item[i].itemname<<"["<<j+1<<"]: ";
getline(cin,item[j].brand);
cout<<endl;
cout<<"Price: Php ";
cin>>item[j].items.price;
cout<<"Stock: ";
cin>>item[j].items.stock;
cout<<"Sold: ";
cin>>item[j].items.sold;
}
}
cout<<"PROD NO. PRODUCT NAME PRICE STOCK SOLD LEFT"<<endl;
for(i=0;i<prodqty;i++)
{
cout<<left<<setw(13)<<"["<<i+1<<"]"
<<setw(12)<<item[i].itemname
<<setw(14)<<item[i].brand
<<endl;
for(j=0;j<brandqty;j++)
{
item[j].items.left=item[j].items.stock-item[j].items.sold;
cout<<left<<setw(10)<<item[j].items.price
<<setw(10)<<item[j].items.stock
<<setw(10)<<item[j].items.sold
<<setw(10)<<item[j].items.left
<<endl;
}
}
}
I want to fix the display just like on the image above.
I did some array but it did not display other items I input.
within the DO-WHILE loop the == and != operators have "no match"
#include <iostream>
using namespace std;
int main()
{
string password = "gang";
int input;
cin >> input;
do{
cout<<"Enter password here: "<<endl;
if(input == password){
break;
}
else{
cout<<"Password incorrect"<<endl;
}
}
while(input != password);
cout<<"Password correct"<<endl;
return 0;
}
Error:
C:\Users\New User\Desktop\c++\666666666666658uttu\main.cpp|18|error: no match for 'operator==' (operand types are 'int' and 'std::__cxx11::string {aka std::__cxx11::basic_string<char>}')|
Because you are comparing a string with a int
You're reading the input in as an int, but comparing it to a string.
Do this
#include <iostream>
using namespace std;
int main()
{
string password = "gang";
string input; // <<<<<<HERE
cin >> input;
do{
cout<<"Enter password here: "<<endl;
if(input == password){
break;
}
else{
cout<<"Password incorrect"<<endl;
}
}
while(input != password);
cout<<"Password correct"<<endl;
return 0;
}
The program in any case is wrong.:)
The variable input is declared as having the type int.
int input;
Then you are trying to compare the entered integer with an object having the type std::string.
string password = "gang";
// ...
if(input == password){
Moreover the loop is infinite because the variable input even if it was declared as having the type std::string is not changed within the loop.
Also you should include header <string>.
The program can look the following way
#include <iostream>
#include <string>
int main()
{
std::string password = "gang";
std::string input;
do
{
std::cout << "Enter password here: ";
if ( not ( std::cin >> input ) or ( input != password ) )
{
std::cin.clear();
std::cout << "Password incorrect\n";
}
} while ( input != password );
std::cout << "Password correct" << std::endl;
return 0;
}
So I just started learning C++ and My professor briefly went over Address (&) and Dereference (*) Operators. I'm not fluent in C++ but i have been searching around for parts and using common knowledge to combine into this code. It fails to build so Please Help!
Assignment- Write a program that keeps reading in strings of varied sizes. If an input string has length greater than one store it in a vector. When an input string has length one (a single character) you will output the string stored in your vector that has the first letter matching the input character. Keep doing this while you read string "quit".
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
string input;
char* output;
vector<string> name;
while (input != "quit") {
cin >> input;
if (input.length == 1) {
for (int i = 0; i < name.size; i++) {
output = &name[i].at(0);
if (input == output) {
cout << name[i];
}
}
}
else {
name.push_back(input);
}
}
//system("pause");
return 0;
}
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
string input;
vector<string> name;
cin >> input;
while (input != "quit") {
if (input.length() == 1) {
for (int i = 0; i < name.size(); i++) {
if (input[0] == name[i][0]) {
cout << name[i] <<endl;
}
}
}
else {
name.push_back(input);
}
cin >> input;
}
system("pause");
return 0;
}
#include <iostream>
#include <cstring>
using namespace std;
void getInput(char *password, int length)
{
cout << "Enter password: ";
cin >> *password;
}
int countCharacters(char* password)
{
int index = 0;
while (password[index] != "\0")
{
index++;
}
return index;
}
int main()
{
char password[];
getInput(password,7);
cout << password;
return 0;
}
Hi!
I'm trying two things here which I'm unable to do atm. I'm trying to create a char array with unspecified length in main, and I'm trying to count the number of words in the char array in the function countCharacters. But password[index] doesnt work.
EDIT: I'm doing a homework assignment, so I have to use cstrings only.
EDIT2: And I'm not allowed to use the "strlen"-function either.
At first replace
char password[];
by
char password[1000]; // Replace 1000 by any maximum limit you want
And then replace:
cin >> *password;
by
cin >> password;
Also instead of "\0" you should put '\0'
P.S. There is no char array with unspecified length in C++, you should use std::string instead(http://www.cplusplus.com/reference/string/string/):
#include <string>
int main() {
std::string password;
cin >> password;
cout << password;
return 0;
}
I want to know how to make stop a while loop when the user just input an Enter without asking to continue or , here is my code:
int main()
{
bool flag = true;
int userInput;
while(flag){
cout<<"Give an Integer: ";
if(!(cin >> userInput) ){ flag = false; break;}
foo(userInput);
}
}
Thanks in advance.
Try this:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
int userInput;
string strInput;
while(true){
cout<<"Give an Integer: ";
getline(cin, strInput);
if (strInput.empty())
{
break;
}
istringstream myStream(strInput);
if (!(myStream>>userInput))
{
continue; // conversion error
}
foo(userInput);
}
return 0;
}
Use getline. Break if the string is empty. Then convert the string to an int.
for(std::string line;;)
{
std::cout << "Give an Integer: ";
std::getline(std::cin, line);
if (line.empty())
break;
int userInput = std::stoi(line);
foo(userInput);
}
std::stoi will throw an exception on failure, handle that however you want.