Click here to Skip to main content
15,888,113 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hello everyone
i wanted to ask you if you can give me the solution to this example?
because i have tried these codes and i don't think it is right.
the example is this : Write a complete C program that reads 3 variables.
The first variable is an integer named x. The second variable is a float named y and the third variable is a character named z.
Then, print variable x multiplied by 10. variable y divided by 2 and the next character after z (z+1).

What I have tried:

C++
// K.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <stdio.h>	
using namespace std;

int main()
{
	int x = 2;
	cout << "2*10=" << 2 * 10 << endl;
	float y = 1.5;
	cout << "1.5/2=" << 1.5 / 2 << endl;
	char z =5 ;
	cout << "z*(z+1)=" << z*(z+1)<< endl;
    return 0;
}
Posted
Updated 2-Sep-16 11:16am
v3
Comments
Philippe Mori 2-Sep-16 18:18pm    
Maybe you have slept too much during your course!

C++
int x = 2;
cout << "2*10=" << 2 * 10 << endl;

Bug 1: you are requested to print variable x multiplied by 10. What if suddenly x= 5 ?

C++
float y = 1.5;
cout << "1.5/2=" << 1.5 / 2 << endl;

Bug 2: you are requested to print variable y divided by 2. What if suddenly y= 3.5 ?

C++
char z =5 ;
cout << "z*(z+1)=" << z*(z+1)<< endl;

Bug 3: you are requested to print the next character after z (aka z+1). You are not requested to print z*(z+1).
Advice: z being a char, z='a' would be more likely.

[Update]
C++
int x  ;
float y  ;
char z = 'a';

You should give values to x and y
To know if program is ok, just rub it many times and change the values to see if results are ok.
 
Share this answer
 
v2
Comments
Member 12715808 1-Sep-16 18:31pm    
dear ppolymorphe thanks for the declaration .
so if i use this code will it be correct?
// Write your code here
#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
int x ;
float y ;
char z = 'a';
cout<<"x*10="<<x*10<<endl;
cout<<"y/2="<<y/2<<endl;
cout<<"(z+1)="<<z+1<<endl;
return 0 ;
}
Patrice T 1-Sep-16 18:44pm    
Use Improve question to update your question.
Member 12715808 1-Sep-16 19:01pm    
So
Int x ; and float y;
X and y should = what?
Patrice T 1-Sep-16 19:11pm    
You choose !
You're missing an important requirement:
Write a complete C program that reads 3 variables.

You should use cin to get the values for the 3 variables, not just assign constant values. Like:
C++
int main()
{
  int x;
  float y;
  char z;
  cout << "Enter x (int):";
  cin >> x;
  cout << "Enter y (float):";
  cin >> y;
  cout << "Enter z (char):";
  cin >> z;
  // etc...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900