HELLP!! I am a complete beginner

I want the input to be taken next to the text as shown in the image?
image link: https://imgur.com/EOyzqOT



[/code]


This is my code and I use devc++.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  #include<iostream>
using namespace std;
int main()
{
        int cno;
		char cname[20];
		char adress[20];
		int a,b,c;
		float i;
		cout<<"Customer name :"<<endl;
		cout<<"Customer adress :"<<endl;
		cout<<"Customer Service number :"<<endl;
		cout<<"Costumer Smart card number :"<<endl;
		cout<<"Costumer Phone number :"<<endl;
		cout<<"Customer Bill number :"<<endl;
		cin>>cno;
		cin>>cname;
		cin>>adress;
		cin>>a;
		cin>>b;
		cin>>c;
		cin>>i;

}
Last edited on
Hello punchingmachine,

If you want you input to look like the image. The first thing you will have to do is rearrange the program.

A line like cout<<"Customer name :"<<endl; with the "std::endl" will put the cursor on the next line. The way it should be written is:

1
2
std::cout << "Customer name : ";
std::cin >> cname;


This will print your prompt and leave the cursor at the end of the line. This format will need to be used for all your inputs.

Some other suggestions:

using namespace std; should be avoided as it WILL get you in trouble some day.

If you need your display to look like the image you have shown you will need to include the header file "<iomanip>" and use "std::setw()" to space the output correctly.

A double is preferred over the float for floating point numbers as it stores the number better and gives a better result in calculations.

The header file "<string>" and a std::string would be a better choice over the character arrays.

Hope that helps,

Andy

Edit:
Last edited on
HandyAndy

THANKS. It help me a lot.
Last edited on
Hello punchingmachine,

Now that you have a better understanding this is what I did. Notice that I changed a couple of the variables from "int"s to "std::string"s.

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
#include<iostream>
#include <iomanip>
#include <string>
#include <limits>

//using namespace std;  // <--- Best not to use.

int main()
{
	std::string cname;
	std::string adress;
	std::string phoneNo;
	std::string servCardNo;
	int a{}, b{}, cno{}/*, c{}*/;  // <--- Always initialize your variables.
	double i{};

	std::cout << std::left;

	std::cout << std::setw(40) << "Customer ID :";
	std::cin >> cno;
	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires heder file <limits>.
	
	std::cout << std::setw(40) << "Customer name :";
	std::getline(std::cin, cname);

	std::cout << std::setw(40) << "Customer adress :";
	std::getline(std::cin, adress);

	std::cout << std::setw(40) << "Customer Service number: ";
	std::cin >> a;

	std::cout << std::setw(40) << "Costumer Smart card number: ";
	std::cin >> servCardNo;  // <--- Changed to "std::string" because number may be to big for an int.

	std::cout << std::setw(40) << "Costumer Phone number  ";
	std::cin >> phoneNo;  // <--- Changed to "std::string". Allows for anything other than numbers.

	std::cout << std::setw(40) << "Customer Bill number: ";
	std::cin >> i;
	// Used to clear the input buffer.
	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires heder file <limits>.
	
	//  <--- Used to pause the program to keep the console window open.
	std::cout << "\n\n\n Press Enter to contniue";
	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires heder file <limits>.
	
	return 0;
}


Watch your indenting. Lines 6 - 22 should not be indented that far.

Any other questions let me know.

Hope that helps,

Andy
Topic archived. No new replies allowed.