| View previous topic :: View next topic |
| Author |
Message |
Kotik 1000+ Club

Joined: 23 Aug 2004 Posts: 1101 Location: Sweden
|
Posted: Sat Nov 13, 2004 5:19 am Post subject: Another C++ Program that needs to be corrected. |
|
|
While I was waiting for someone to respond to my other thread i tried to make a program myself. This is what I ended up with.
| Code: |
// This is a simple "signing in" program.
#include <iostream.h>
main()
{
int med; //This is the username. Note: In digits.
char pass[5]; //This is the password.
char correct = Arman; //Pass has to be equal to Arman in case of a successfull sign in.
cout << "Type in your username \n";
cin >> med;
switch (med) { //The switch starts here
case 12: // I chose that 12 should be the correct username.
cout << "Type in your password \n";
cin >> pass;
if (pass == Arman) {
cout << "You have successfully logged in to your account \n";
}
else {
cout << "Your password was incorrect \n";
}
default:
cout << "There is no such username. Try again by starting the program again. \n\n";
}
return 0;
} |
char correct = Arman;
Unidentified character 'Arman'. <--- Thats the error message.
Dont get why it pops up.
If I get that fixed I would be able to hand in 2 programs. That might make a better impression. My aim is an A+.
Thanks in advance! |
|
| Back to top |
|
 |
|
|
Malcolm 100+ Club

Joined: 07 Oct 2004 Posts: 199 Location: Ontario, Canada
|
Posted: Sat Nov 13, 2004 5:28 am Post subject: Re: Another C++ Program that needs to be corrected. |
|
|
first off where you define
| Code: |
char correct = Arman;
|
what the compiler is seeing is:
I'm making a new variable called correct, and I'm going to put the value of Arman into it.
what you probably want is
| Code: |
char correct[5] = "Arman";
|
To say that:
I'm going to make a character array, that has 5 indexes thats named correct and I'm going to fill that array with "Arman" |
|
| Back to top |
|
 |
Kotik 1000+ Club

Joined: 23 Aug 2004 Posts: 1101 Location: Sweden
|
Posted: Sat Nov 13, 2004 8:06 pm Post subject: Re: Another C++ Program that needs to be corrected. |
|
|
| Malcolm wrote: |
first off where you define
| Code: |
char correct = Arman;
|
what the compiler is seeing is:
I'm making a new variable called correct, and I'm going to put the value of Arman into it.
what you probably want is
| Code: |
char correct[5] = "Arman";
|
To say that:
I'm going to make a character array, that has 5 indexes thats named correct and I'm going to fill that array with "Arman" |
I tried that but there was still some sort of an error in the program. I could not figure out what it was. But then I decided to try and change the password to digits instead. That worked fine. The code now looks like this
| Code: |
#include <iostream.h>
main()
{
int med; //Detta ar Medlemnumret.
int pass; //Losenordet som skrivs in sparas pa pass
int correct = 12345; //Pass maste alltsa vara lika med corrct for att inloggningen ska lyckas.
cout << "Type in your membership number : \n";
cin >> med;
switch (med) { //Har borjar switchen
case 12:
cout << "Type in your password : \n";
cin >> pass;
if (pass == correct) {
cout << "You have successfully logged in to your account \n";
break;
}
else {
cout << "Your password is incorrect. Run the program again. \n";
break;
}
default:
cout << ". The membership number is invalid. Access denied.\n\n";
}
return 0;
} |
It works fine now. Thanks for all your help Malcom.
Edit: The membership and password needed to run the program are:
| Code: |
Membership nr : 12
Password : 12345 |
|
|
| Back to top |
|
 |
Kotik 1000+ Club

Joined: 23 Aug 2004 Posts: 1101 Location: Sweden
|
Posted: Sun Nov 14, 2004 3:19 am Post subject: Re: Another C++ Program that needs to be corrected. |
|
|
There is some more help I need:
I wanna make a program that calculates the sum of different variables (marks1,mark2,marks3) and save them in a variable called "total". I'm not sure how the syntax should look like, but searching for a syntax I came across one that was something like this
| Code: |
| inline float total (int marks1;int marks2;int marks3) {return marks1+marks2+marks3} |
There still are some errors in this line. What are they?
Then I have some questions about the program's syntax. What is inline and why do we use return?
Any help appreciated.
Arman |
|
| Back to top |
|
 |
Malcolm 100+ Club

Joined: 07 Oct 2004 Posts: 199 Location: Ontario, Canada
|
Posted: Sun Nov 14, 2004 4:25 am Post subject: Re: Another C++ Program that needs to be corrected. |
|
|
how I'd do it is by using input statements. I'v never used a function anything like the one you have above. Looks like they're doing some sort of inline function where the variables would have to already be assigned, then they'd be passed through the inline function; funky.
by input:
| Code: |
#include <stdio.h>
int main(){
float a, b, c, total;
printf("Enter 3 values to be added: ");
scanf("%f %f %f", &a, &b, &c);
total = a + b + c;
printf("The sum of your variables is %f.\n", total);
return 0;
}
|
{EDIT}
When I say inline function I mean a function thats already contained inside of another function, something that I've never seen and didn't know I could do.
{EDIT x2}
found some documentation:
http://www.greenend.org.uk/rjk/2003/03/inline.html |
|
| Back to top |
|
 |
Kotik 1000+ Club

Joined: 23 Aug 2004 Posts: 1101 Location: Sweden
|
Posted: Mon Nov 15, 2004 2:05 am Post subject: Re: Another C++ Program that needs to be corrected. |
|
|
Now I have 2 kick ass C++ programs (of my level) and I'm 100% sure I will get an MVG (A+) for them in the final result.
Thanx Malcom! |
|
| Back to top |
|
 |
Ceyhun
Joined: 15 Aug 2005 Posts: 2
|
Posted: Tue Aug 16, 2005 11:26 am Post subject: For calculating the sum. |
|
|
In fact, in such a code you do not need inline. Because for C performance is very important designer need to give it to C almost all the power of assembly. Inline means in assembly code extract the function and only affect the speed and memory size of code. In a processor like Pentium you cannot observe this. And No matter use or donot use. (Because such such directives C is ideal for very slow processor inside the phone or remote control)
Acc. to me dont use. You can use such a code directly.
| Code: |
#include <stdio.h> /*This file is standard input output file.*/
float total (int mark1, int mark2, int mark3)
{
/*This function take 3 integer and generate and send 1 float*/
return mark1 + mark2 + mark3; /*generate and send(or return) the result*/
} /*End of the function
int main() /*This function call automatically when program start*/
{
float result = total(4, 5, 2);
printf("Result=%f", result);
retrun 0;
}
|
I think this is the code you need. Note you see the result in scientific notation (1.1e1). Some thing like taht I am not sure. you set the display notation inside printf eith char %: %f but if you dont like you must look the printf function.
Good Luck |
|
| Back to top |
|
 |
|