Hello Programhelpa,
Welcome to the forum.
PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.
Since you asked. There are many problems with your code. Thomas1965 has pointed out two points.
From the top:
You are using "std::string"s in the program, but you did not include the header file "<string>".
"conio.h" is not a standard C++ header file and some IDEs and compilers do not support this header file these days, so some people can not use your code.
The same applies to "<windows.h>". This wil confine your code to Windows computers only.
The line "using namespace std" id a bad idea. This
WILL get you in trouble some day. It is best to learn what is in the standard namespace and how to qualify these variables or functions like "std::cout", "std::cin" and "std::endl". The sooner you learn this the better you will be prepared when your first job tells you that you can not use "using namespace std".
You should use better variable names to make your code easier to understand as you will see later.
Line 13, of the following code, you should ALWAYS initialize your variables. The empty set of {} is the simplest way. You can also put a number inside the { 10 } if you need something other than zero.
The "gotoxy" function is OK for the most part. If I remember correctly "CORD.X" and "CORD.Y" are defined as a short type and you are setting a short equal to an int. This will work, but could be a problem some day. You should define your "x" and "y" as short.
I main you prompt the user for input:
Name is OK.
Book Type is a problem. First you need to give the user an idea of what needs to be entered. Second the variable should be defined as "char" not a "string".
"Borrow (date): " is misleading. I think of a date as "12/24/20017", but this variable is an int and would only pick up the "12". With what your code is I do nt understand what number to put here for a date. the same applies to "Back (date): ". You need to give an example of what is needed here for these two inputs.
The if statements I have not worked with yet. In your version
if (b = "a", "A")
you set b equal to "a". Not what you want refer to Thomas1965's example. I believe what comes after the comma operator is just disregarded because it does not know what to do with it.
Line 62 will give you an idea of what is happening on line 63 and why line 64 is the wrong answer.
Line 67 "system" anything is best to avoid because of security risks.
Lines 69 to 74 I changed some of the variable names. here is a good example of using a more descriptive variable name.
Line 74 I changed to give you a better idea of what you re outputting.
Lines 76 and 77 I added to keep my console window open so the program does not end to soon. If you do not need these lines put comments on them or delete them.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
|
#include <iostream>
#include <string> // <--- Added.
#include <conio.h> // <--- Do not believe this is used for anything. I used for _getch().
#include <windows.h>
//using namespace std; // <--- Best not to use.
std::string name;
std::string bookType;
std::string typeOfBook;
int lp{}; int bp{}; int dp{}; int tax{}; int BorrowDate{}; int backDate{}; // <--- ALWAYS initialize your variables.
void gotoxy(int x, int y)
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
int main()
{
gotoxy(0, 0); std::cout << "Student name : " << std::endl;
gotoxy(0, 1); std::cout << "Book type : " << std::endl;
gotoxy(0, 2); std::cout << "Borrow (date): " << std::endl;
gotoxy(0, 3); std::cout << "Back (date): " << std::endl;
gotoxy(15, 0); // <--- Changed to 15 for better position.
std::cin >> name;
gotoxy(13, 1);
std::cin >> bookType;
gotoxy(15, 2); // <--- Missing closing ). Changed to 15.
std::cin >> BorrowDate;
gotoxy(13, 3);
std::cin >> backDate;
if (bookType = "a", "A")
{
typeOfBook = "Physics";
bp = 3;
dp = 20;
}
else
{
if (bookType = "P", "p")
{
typeOfBook = "Biology";
bp = 2;
dp = 30;
}
else
{
typeOfBook = "Social";
bp = 1;
dp = 10;
}
}
// -16 = 6 - 22
lp = backDate - BorrowDate;
tax = lp * dp;
system("CLS");
std::cout << "Student name :" << name << std::endl;
std::cout << "Book type :" << typeOfBook << std::endl;
std::cout << "Long borrow :" << lp << " day" << std::endl;
std::cout << "Maximal date :" << bp << " day" << std::endl;
std::cout << "Tax :" << tax << " piece of coins" << std::endl;
std::cout << "Book type: " << bookType << ", Borrow date: " << BorrowDate << " Back date: " << backDate;
std::cout << "\n\n Press anykey to continue: ";
_getch();
return 0;
}
|
Hope that helps,
Andy