Help with c++ code - Geeks to Go Forums

Jump to content

Log in Register Register Malware removal guide How it works

Help with c++ code For your information, this is my first code

#1 Datasmurfen

  • Group: Member
  • Posts: 106
  • Joined: 08-October 10

Posted 31 March 2011 - 02:05 AM

//Include the header
#include <stdio.h>

//My main function
int main() {
  //Print "Hello, World!" to the screen
  printf("Hello, World!");
  //Get keyboard input
  getchar();
  printf("Hello, World!");
	getchar();
  	int x = 0;
  printf("Please enter a number: ");
  scanf("%d", &x);
  printf("You entered %d.\n\n", x);
   printf("Pres enter to continue...", x);
  getchar();
  getchar();
printf("Please enter your name: ");
  scanf("%d", &x);
  printf("You entered %d.\n", x);
   printf("Pres enter to continue...");
getchar();
getchar();
printf("Pres enter to continue...");
	getchar();
  	getchar();
 
}

My problem is when i input "your name "i only got some numbers up.
What i do wrong?

#2 OldTimer

  • Group: Global Moderator
  • Posts: 3,261
  • Joined: 11-March 05

Posted 31 March 2011 - 06:06 PM

Look at these lines:

printf("Please enter your name: ");
scanf("%d", &x);
printf("You entered %d.\n", x);

What is happening with these?

Cheers.

OT

#3 Datasmurfen

  • Group: Member
  • Posts: 106
  • Joined: 08-October 10

Posted 31 March 2011 - 11:50 PM

Thx for helping me OldTimer.
I think the %d variabel is the same as the number input i have. Maybe i should try to use f.eks %o variabel.

But i having problem with find out where and why the x is there.

I follow'ed In the g2g tutorial located here: http://www.geekstogo...02#entry1988102

#4 OldTimer

  • Group: Global Moderator
  • Posts: 3,261
  • Joined: 11-March 05

Posted 01 April 2011 - 07:06 AM

%d isn't a variable as such, it's a formatting parameter. What will it accept as input?

The same for %o. What will it accept as input?

Unless you are required to use scanf and printf for some reason, what better method(s) of getting input from the console and sending output to the console are there in C++?

Cheers.

OT

#5 Datasmurfen

  • Group: Member
  • Posts: 106
  • Joined: 08-October 10

Posted 04 April 2011 - 01:33 AM

View PostOldTimer, on 01 April 2011 - 07:06 AM, said:

%d isn't a variable as such, it's a formatting parameter. What will it accept as input?

The same for %o. What will it accept as input?

Unless you are required to use scanf and printf for some reason, what better method(s) of getting input from the console and sending output to the console are there in C++?

Cheers.

OT


%d stands for number input
%0 i have problems finding a answer on this


I found this script:
// i/o example #include <iostream> using namespace std; int main () { int i; cout << "Please enter an integer value: "; cin >> i; cout << "The value you entered is " << i; cout << " and its double is " << i*2 << ".\n"; return 0; }

The reason for i used printf and scanf is because of this tutorial someone have made: http://www.geekstogo...ing-c-tutorial/

I will thank you for takeing the time to help me to learn c++. OldTimer

#6 OldTimer

  • Group: Global Moderator
  • Posts: 3,261
  • Joined: 11-March 05

Posted 04 April 2011 - 06:47 AM

Ah, tht explains it. That G2G turotial is using C, not C++. While C++ is mostly backward compatible with C the opposite is not true. C++ can handle simple input/output much more efficiently than the C scanf/printf through cin/cout.

%o is used for octal integers.

Do a google for C++ tutorial and you should get a good-sized hit list. www.cplusplus.com has a pretty good beginning tutorial for C++, try that one as a starting point.

Cheers.

OT

#7 bretwt91

  • Group: Member
  • Posts: 14
  • Joined: 18-June 11

Posted 18 June 2011 - 04:23 AM

hI
Type the following code and see what happens. I m sure this will solve your problem. I

printf("Please enter your name: ");
scanf("%s%d", &x);
printf("You entered %d.\n", x);

#8 SpySentinel

  • Group: Retired Staff
  • Posts: 5,152
  • Joined: 22-September 07

Posted 20 June 2011 - 05:38 PM

Quote

Unless you are required to use scanf and printf for some reason, what better method(s) of getting input from the console and sending output to the console are there in C++?


You can also try cout << and cin >>

Share this topic: