Trying to make a login program [closed] - c++

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I'm trying to make a login page. When the program starts, the user has to type Guest and Password 1234 and he can edit his/her account. However, when I try to run it, it says:
Line 15 "Error incompatible types in assignment of 'const char[6]' to 'char[20]'
Line 16 "Error incompatible types in assignment of 'const char[5]' to 'char[20]'
I think it has to do with pointers but I am still a c++ newbie so I am having a hard time to understand pointers
#include <iostream>
#include <cstring>
#include <stdio.h>
#include <string.h>
using namespace std;
const int LINE_LENGTH=20;
const int ID_LENGTH=8;
struct profile{char user[20];char password[20];double CGPA; int ID;};
int main(){
int count=0, i;
profile student[10];
student[0].user="Guest"; //Line 15
student[0].password="1234"; //Line 15
char signupName[20];
char signupPassword[20];
while (count==0)
{
cout << "#############################################\n";
cout << " Welcome to my program! \n";
cout << " Sign up to get started \n\n\n";
cout << " If you are starting, use username 'Guest' \n";
cout << " and password '1234' \n\n";
cout << "Username: ";
cin >> signupName;
cout << "Password: ";
cin >> signupPassword;
cout << "#############################################\n";
for (i=0;i<11; i++)
{
if(strcmp(signupName,student[i].user)==0 && strcmp(student[i].password,signupPassword)==0)
{
count++;
}
}
if(count==0)
{
system("cls");
cout<<"Your username and/or password is incorrect\n";
}
}
system("cls");
}

You need two minor changes to your code! First, as Francois Andrieux says, you can't assign char array strings with = ...
// student[0].user = "Guest";
// student[0].password = "1234";
strcpy(student[0].user, "Guest");
strcpy(student[0].password, "1234");
Second, your for loop runs once to often:
// for (i = 0; i < 11; i++)
for (i = 0; i < 10; i++) // Note: The last element in an array of 10 is x[9]!

Related

C++ Need help getting an integer from the user and making sure it is between 2 other integers [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I am new to c++ and cant seem to figure out how to simply get an integer from the user and make sure it is between 0-15. Here is my code so far:
When I run the code it only prints Hello world
int main()
{
int greetAndGet();
cout << "Hello";
return 0;
}
int greetAndGet()
{
int i;
cout << "\nPlease give an integer in [1,15]" << endl;
cin >> i;
cout << endl;
}
int greetAndGet(); is a forward declaration of a function, not a call.
Write greetAndGet(); instead.
Note further that a function should be defined/declared before any call to it. So either place the function definition before main, or write
int greetAndGet(); // forward declaration
int main()
{
greetAndGet();
cout << "Hello";
return 0;
}
...
As pointed out in another answer, int greetAndGet() is a forward declaration that you probably intended to be a call; though you do want to forward declare it before main. As for testing the range of the entered value, you could use a loop to check if it is in the range. I think what you want is this:
int greetAndGet();
int main()
{
int num = greetAndGet();
cout << "Hello";
return 0;
}
int greetAndGet()
{
int i;
cout << "\nPlease give an integer in [1,15]" << endl;
do {
cin >> i;
if(i < 1 || i > 15)
{
cout << "Number not in [1,15], please try again" << endl;
}
} while(i < 1 || i > 15);
cout << endl;
return i;
}
I'm not sure what you want to do with the number, but this should get you the entered number.

Why can't compare two strings in if condition? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Why can't compare two strings in if condition?
#include <iostream>
#include <string.h>
using namespace std;
int main() {
string sexo[20], feminino;
feminino = "f";
for (int i = 0; i < 3; i++) {
do {
cout << endl << "enter your " << i + 1 << "sexo: ";
cin >> sexo[i];
if (strcmp(sexo[i], feminino)==0){ // problem in here
cout << "that's ok" << endl;
}
} while (nome[i] == "0");
}
return 0;
}
You've been reading "tutorials" for C, or "tutorials" for C++ that actually teach you a terrible and outdated mix of C and C++.
The function strcmp is from the C Standard Library, and does not operate on the C++ std::string type.
To compare two std::strings, simply write:
if (sexo[i] == feminino) {
I find it hard to believe that your C++ book does not teach you this.
These are a few correct ways to compare these strings (in reverse order of preference)
if (strcmp(sexo[i].c_str(), feminino.c_str()) == 0) {
if (sexo[i].compare(feminino)) == 0) {
if (sexo[i] == feminino) {
You are using the wrong compare function. What you are using works with char * (it is used in C) but here you have std::string so you have to use std::string::compare()
Change your code to this:
#include <iostream>
#include <string.h>
using namespace std;
int main() {
string sexo[20], feminino; // problem in here
feminino = "f";
for (int i = 0; i < 3; i++) {
do {
cout << endl << "enter your " << i + 1 << "sexo: ";
cin >> sexo[i];
if (sexo[i].compare(feminito) == 0){
cout << "that's ok" << endl;
}
} while (nome[i] == "0");
}
return 0;
Note that you can also use sexo[i] == feminito as you have relational operators for std::string (see here for examples)

Dynamic array problems [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Ok my problem is, in my dynamic array function I have an array that gives me the error below.
#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>
#include <fstream>
using namespace std;
class Revenue
{
};
static int Track_Num_Divisions = 1;
static int Track_Quart_Revenue = 1;
void Program_loop()
{
{
string D;
string DN;
int N;
double TS;
double TC;
double P;
int arry;
cout << "how many Revenue tiers do you want?: "; cin >> arry;
Revenue* rev = new Revenue[arry];//dynamic array
for (int i = 0, Track_Num_Divisions = 1;Track_Num_Divisions, i < arry; i++,Track_Num_Divisions++ )
{
Revenue& rev = rev[i];// THIS IS THE ERROR <<<<
cout << " " << endl;
cout << "Revenue #"<<Track_Num_Divisions << endl;
cout << "===========" << endl;
cout << "<< Ok what is your division name?: " << endl; cin >> D;
string set_Division_name(D);
cout << "<< What is your division number?: " << endl; cin >> DN;
string set_Division_number(DN);
while (DN.size() != 4)
{
cout << "<< Sorry! Your Division Number cannot exceed or be short of 4. " << endl; cin >> DN;
}
delete[] rev;
}
Gives this error in function Dynamic_array
I think the problem lies in this code>> Revenue& rev =rev[i]:
Error 1 error C2676: binary '[' : 'Revenue' does not define this operator or a conversion to a type acceptable to the predefined operator
Error 2 IntelliSense: no operator "[]" matches these operands
operand types are: Revenue [ int ]
What should I do?
I am kinda new to this Website still learning the ropes of proper format.
This line is the problem:
Revenue& rev = *rev[i];
You're dereferencing the value returned by rev[i], but rev[i] is not a pointer or a class with an overloaded operator*. It's a Revenue&.
There's no need for derferencing anything here, just write it as:
Revenue& rev = rev[i];

Why does the loop loop itself when "else" is triggered? Is this because of things called memory allocation? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm just a beginner and trying out some code that my teacher taught us to use and things from the textbook.
This program is designed to be for the user to enter in their name and enter in the password as what the system asks them to put down.
Can somebody explain to me why this loop keeps looping itself infinitely when else is triggered?
Also, what does the cin.ignore do to the memory of the char name? Why is 80 better than 20?
AND, why aren't the random numbers actually random? Every time I run it, the numbers are the same.
Thank you all so much!
#include <iostream>
#include <cstdlib>
using namespace std;
int main ()
{
char name[20];
int pwd, rand1, rand2;
for (int i=0;i<1; i++)
{
cout<<"Name: ";
cin.get(name, 20);
cin.ignore(80, '\n');
cout<<endl;
srand(rand() % 1000);
rand1 = (rand() % 21);
rand2 = (rand()%6);
cout<<"Password: "<<rand1<<"*"<<rand2<<"= ";
cin>>pwd;
if(pwd == rand1*rand2)
{
cout<<endl<<"Welcome to our main page, "<<name<<"."<<endl;
}
else
{
cout<<"Wrong password, type again." <<endl;
i--;
}
}
return 0;
}
First up formatting of code will help you understand better.
Also avoid using namespace std, its bad practice and clutters the global scope with names. Instead use using std::xxxx if you dont want to write std::cout, std::cin, etc every time.
Reformatted code:
#include <iostream>
#include <cstdlib>
using std::cin;
using std::cout;
using std::endl;
int main ()
{
char name[20];
int pwd, rand1, rand2;
for (int i = 0; i < 1; i++) {
cout << "Name: ";
cin.get(name, 20);
cin.ignore();
cout << endl;
srand(rand() % 1000);
rand1 = (rand() % 21);
rand2 = (rand() % 6);
cout << "Password: " << rand1 << "*" << rand2 << "= ";
cin >> pwd;
cin.ignore();
if(pwd == rand1*rand2) {
cout << endl << "Welcome to our main page, " << name << "." << endl;
} else {
cout << "Wrong password, type again." << endl;
i--;
}
}
return 0;
}
Secondly as you can see in the above code the line cin.ignore(); has been added after cin >> pwd. Before your code was getting cin >> name, leaving '\n' in the input, ignoring '\n', getting cin >> pwd, leaving '\n' in input, looping and reading input as empty with a '\n', leaving another '\n' in input, first '\n' is removed by ci.ignore(), second '\n' read by cin >> pwd, ... etc. Or at least this is how I understand it.
Somebody has answered the first question:Because when you i--, the i in the for loop keeps decreasing and then increasing.-By Gasim
Then, if your input is longer than 20, the program may stop. So you need cin.ignore(80, '\n') to ignore the excess input. The number 80 is just a big number. You can replace it with another number-only if it's big enough.
You are supposed to use srand with time. srand(time(null)) may help.

I am getting C++ syntax error [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
This script is supposed to read chars from keyboard, store them into arrays, and then output them:
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
void storeArraysintoStruct(char[], int);
int main()
{
char test[] ="";
int a = 0;
storeArraysintoStruct(test, a);
system("pause");
return 0;
}
void storeArraysintoStruct(char test[], int a)
{
int n;
cout << "Enter number of entries: " << endl;
cin >> n;
int i = 0;
for (i=0, i<n, i++)
{
cout << "Enter your character: " << endl;
cin.getline(test, n);
}
while (i < n)
{
cout << test[i] << endl;
i++;
}
}
Edit: fixed it:
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
void storeArraysintoStruct(char[], int);
int main()
{
char test[40] = "";
int a = 0;
storeArraysintoStruct(test, a);
system("pause");
return 0;
}
void storeArraysintoStruct(char test[], int a)
{
int n;
cout << "Enter number of entries: " << endl;
cin >> n;
int i;
for (i=0; i < n; i++)
{
cout << "Enter your character: " << endl;
cin >> test[i];
if (test[n-1])
{
cout << endl;
}
}
i =0;
while (i < n)
{
cout << test[i] << endl;
i++;
if(test[n-1])
{
cout << endl;
}
}
}
However, I am getting the errors Expected: primary expression before ")" and ";" before while. Any help will be greatly appreciated.
Edit: The script doesn't work as expected, for it doesn't output the stored characters. Any advice would be greatly appreciated.
The syntax error has already been pointed out in the comments. Also, as it has been mentioned, you never reset i after for loop, which prevents your while loop from running.
However, you have to also take in mind that this
char test[] = "";
allocates array test of only 1 character long. You cannot put more than one character of data into that array. In other words, your storeArraysintoStruct is sure to overrun the array and fall into undefined behavior territory.
In you want to preallocate a larger buffer for future use in storeArraysintoStruct, you have to specify the size explicitly. For example
char test[1000] = "";
will make test an array of 1000 characters. Of course, regardless of how large the array is, it is your responsibility to observe the size limit.
P.S. What is the point of that parameter a, if you never use it inside storeArraysintoStruct?