It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Board index Programming C and C++ Forum

Another C++ Program that needs to be corrected.

Another C++ Program that needs to be corrected.

Postby Kotik on Sat Nov 13, 2004 4:19 am

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: Select all
// 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!
DEVPPL : News Publisher
User avatar
Kotik
1000+ Club
 
Posts: 1086
Joined: Mon Aug 23, 2004 12:25 am
Location: Sweden

Postby Malcolm on Sat Nov 13, 2004 4:28 am

first off where you define
Code: Select all
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: Select all
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"
User avatar
Malcolm
100+ Club
 
Posts: 198
Joined: Thu Oct 07, 2004 9:53 pm
Location: Ontario, Canada

Postby Kotik on Sat Nov 13, 2004 7:06 pm

Malcolm wrote:first off where you define
Code: Select all
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: Select all
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: Select all
#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: Select all
Membership nr : 12
Password : 12345
DEVPPL : News Publisher
User avatar
Kotik
1000+ Club
 
Posts: 1086
Joined: Mon Aug 23, 2004 12:25 am
Location: Sweden

Postby Kotik on Sun Nov 14, 2004 2:19 am

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: Select all
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
DEVPPL : News Publisher
User avatar
Kotik
1000+ Club
 
Posts: 1086
Joined: Mon Aug 23, 2004 12:25 am
Location: Sweden

Postby Malcolm on Sun Nov 14, 2004 3:25 am

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: Select all
#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
User avatar
Malcolm
100+ Club
 
Posts: 198
Joined: Thu Oct 07, 2004 9:53 pm
Location: Ontario, Canada

Postby Kotik on Mon Nov 15, 2004 1:05 am

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!
DEVPPL : News Publisher
User avatar
Kotik
1000+ Club
 
Posts: 1086
Joined: Mon Aug 23, 2004 12:25 am
Location: Sweden

For calculating the sum.

Postby Ceyhun on Tue Aug 16, 2005 10:26 am

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: Select all
#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
Ceyhun
 
Posts: 2
Joined: Mon Aug 15, 2005 10:59 am


Who is online

Users browsing this forum: No registered users and 0 guests