Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
Photo

Help with c++ code


  • Please log in to reply

#1
Datasmurfen

Datasmurfen

    Member

  • Member
  • PipPipPip
  • 106 posts
//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?
  • 0

Advertisements


#2
OldTimer

OldTimer

    Global Moderator

  • Global Moderator
  • 3,273 posts
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
  • 0

#3
Datasmurfen

Datasmurfen

    Member

  • Topic Starter
  • Member
  • PipPipPip
  • 106 posts
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
  • 0

#4
OldTimer

OldTimer

    Global Moderator

  • Global Moderator
  • 3,273 posts
%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
  • 0

#5
Datasmurfen

Datasmurfen

    Member

  • Topic Starter
  • Member
  • PipPipPip
  • 106 posts

%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

Edited by Datasmurfen, 04 April 2011 - 01:35 AM.

  • 0

#6
OldTimer

OldTimer

    Global Moderator

  • Global Moderator
  • 3,273 posts
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
  • 0

#7
bretwt91

bretwt91

    Member

  • Member
  • PipPip
  • 14 posts
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);
  • 0

#8
SpySentinel

SpySentinel

    R.I.P.

  • Retired Staff
  • 5,152 posts

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 >>
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP