tags:

views:

114

answers:

4

Hello!

I'm an absolute beginner to programming and i'm just doing some exercises exercises for the beginning.

First of all, i'm using Visual C++ 2010 to compile C-Code. I just create a new project and choose an empty console application. After that, I create a ressource file named test.c and change in the file properties the elementype to C/C++ Compiler and compile as C++ Code, so that i can use #include <iostream> for the std::cin.get() command. Now the code:

#include <stdio.h>
#include <iostream>

int main() 
{
   int number1, number2;
   int sum;

   puts("Enter number 1 please:");
      scanf_s("%d",&number1);
   puts("Enter number 2 please:");
      scanf_s("%d",&number2);

   std::cin.get();
   std::cin.get();  //(1)

   sum = number1 + number2;
      printf("The average is %f\n", sum/2);

return 0; 
}

Now my problem ist that the "std::cin.get()" command is just ignored. Afer typing in the two numbers the program just stops and the console window closes.

Any idea where the problem is?

I have another question please.

Since my problem with holding the console open is solved (1), now my printf() gives me just zeros as output. I want to have a float number as output but no matter what i type in as number1 and number2 i always get "0.000000".

Since i'm still working on my little program to verify the input before it is accepted, i have another question please.

I want to use the following code just to check the input.

#include <stdio.h>
#include <iostream>
#include <ctype.h>

int main() 
{
   int number1, number2;
   int sum;

   puts("Enter number 1 please:");
      scanf_s("%d",&number1);

   if (isdigit(number1)) 
   {
   puts("Enter number 2 please:");
      scanf_s("%d",&number2);
   }
   else 
   {
   puts("Your input is not correct. Enter a number please.");
   }

   std::cin.get();
   std::cin.get();

   /*



   sum = number1 + number2;
      printf("The average is %f\n", sum/2);  */

return 0; 
}

Well it doensn't work. I type in a digit and my response is "Your input is not...". I have used the search and found the following: Check if User Inputs a Letter or Number in C. Unfortunately the suggestions doesn't help me.

+10  A: 

It's not ignored. When you type your second number, then hit enter, it puts your number plus a newline character in the input stream. scanf removes the number, but leaves the newline character alone. When you call cin.get(), since there's a character in the stream, it doesn't wait for your input.

PigBen
Thanks a lot! I have just repeated the line "cin.get()" and it works now.
Ordo
A: 

PigBen has already given you a good explanation of where you err. However, I have some additonla points to make about your program which won't fit into a comment:


You are mixing C and C++ input. Why? What's wrong with std::cin >> number1?
When you change number1 to double, you need to remember to change the formatting string in scanf(), too, while with IO streams the compiler will figure out everything for you. With streams and C++' strings, containers and other data structures, it's much harder to do something that compiles, but invokes the dreaded Undefined Behavior at run-time.


Also note that you do not check whether your inputting operations succeed. What happens if I invoke your program, and instead of passing it numbers, I enter non-digits? Never use input from users, files, or other externals sources unverified.
With IO streams, the input operator >> returns (a reference to) the stream, and you can use streams as if they were booleans, so you can do

if(std::cin >> number1)
  // input succeeded

or

if( !(std::cin >> number2) )  // note the negation operator !
  // input error

to check.

Streams enter a bad state internally after input/output errors. Any further IO operations will fail on a stream that had encountered an error. Therefore, if you want, you can delay input verification until all input operations are done:

std::cout << "Enter number 1 please:";
std::cin >> number1;
std::cout << "Enter number 2 please:";
std::cin >> number2;

if(!std::cin) 
  // input error

However, remember to always verify input before you first use it.

Note that I didn't check the output for errors. That's because it's hard to imagine something going wrong with output to the console. (And what would you do about it? Print an error message?) However, if you write into a file, remember to check output, too. It's easy for a file operation to go wrong.

sbi
Thanks for your advices. I will try to implement an input and output verification in my little "program".
Ordo
A: 

In answer to your modified question it is because you are using ints for division. Change int sum to float sum and everything should be fine.

graham.reeds
Thank you! This solution works too.
Ordo
A: 

To answer your modified question: you're using the %f printf() format with an int, and that doesn't work. If you want to print out floating-point, you need to pass a double. You could print out (double)sum / 2 or even sum / 2.0, both of which yield doubles. (No, a float doesn't work the same for a variadic function like printf().) As it is, you're passing what is probably a four-byte type and telling printf() to treat it as an eight-byte type of different format, so it's no wonder you're not getting the expected results.

Alternately, you could switch to C++ iostreams, which save you the problem of matching types and knowing the default promotions. You'd still get an integer from sum/2, and that would drop any one-half, but it would be the right result.

David Thornley
Thank you. All of your suggestions work well.
Ordo