Error C2078: too many initializers - c++

Why does this code not work? My IDE is Visual Studio 2013.
#include <stdio.h>
float tempatureGuide(float F, float C);
#define FREEZING_PT 32.0f
#define SCALE_FACTOR (5.0f/9.0f)
int main(void)
{
float fahrenheit = 0.0;
float celsius = 0.0 ;
int convertTemp;
printf ("Enter 0 to calculate Celsius or 1 to calculate Fahrenheit: ");
scanf ("%d", &convertTemp);
if (convertTemp == 0)
{
// compute Celsius
printf("Enter Fahrenheit temperture: ");
scanf("%f", &fahrenheit);
celsius = ((fahrenheit - FREEZING_PT) * SCALE_FACTOR);
printf("Fahrenheit = %f and Celsius = %f\n", fahrenheit, celsius);
float tempatureGuide(fahrenheit, celsius); // Error here
}
else
{
// compute fahrenheit
printf("Enter the temperature in degrees fahrenheit\n\n");
scanf("%f", &fahrenheit);
celsius = (SCALE_FACTOR)* (fahrenheit - FREEZING_PT);
printf("The converted temperature is %f", celsius);
float tempatureGuide(fahrenheit, celsius); // and here
}
return (0);
}
float tempatureGuide(float F, float C){
if (F < 32 || C < 0)
printf("It is freezing!");
else if (F <= 60 || C <= 16)
printf("It is cold");
else if (F >= 70 || C >= 21)
printf("It is just right");
else if (F >= 82 || C >= 28)
printf("It is warm");
else if (F > 95 || C > 35)
printf("It is hot");
else
printf("Please enter a number!");
return (0);
}
The goal here is to add to the converting temperature project I did earlier and add an if else statement function to it that comments on the temp. The error I get is
Error 3 error C2078: too many initializes
on both the lines where I call my function. I searched for an answer but couldn't find any.

This line looks like an attempt at a C++ initialization of a float with one too many arguments (hence the "too many initializers" error), not like a function call.
float tempatureGuide(fahrenheit, celsius);
Presumably you want to call the function and store the result in a variable:
float temp = tempatureGuide(fahrenheit, celsius);
Or just call the function and ignore the return value:
tempatureGuide(fahrenheit, celsius);
Especially since your function always returns 0, so one might question the need for a non-void return type.

You need to call a function
float tempatureGuide(float fahrenheit, float celsius) { //...}
as
float retval = tempatureGuide(fahrenheit, celsius);
or least
tempatureGuide(fahrenheit, celsius); // not need to use return value
Only.

That is just a simple error. Please change 2 lines of code where you are calling tempatureGuide(fahrenheit, celsius); function as follows.
float tempatureGuide(fahrenheit, celsius); --> float ret = tempatureGuide(fahrenheit, celsius);
I tested the same in my VS2013 as you are using the same with the mentioned changes. I am able to compile and run it successfully. See the attachment.

Just call the function and return it to a variable of the same type.

Related

A simple calculator using C about conversion of Fahrenheit to Celsius, and vice versa

Good day! I tried to make a simple calculator using C for my first project about conversion between Fahrenheit to Celsius, and vice versa. But it's not working, can someone tell me what I miss?
Here's my code:
#include <stdio.h>
int main()
{
double temp, fahrenheit, celsius;
char answer[2];
printf("Type 'CF' if you want to convert from celsius to fahrenheit, and 'FC' if you want to convert from fahrenheit to celcius: ");
fgets(answer, 2, stdin);
fahrenheit = (temp * 1.8) + 32;
celsius = (temp - 32) * 0.5556;
if(answer == "CF"){
printf("Type the temperature here: ");
scanf("%lf", &temp);
printf("Answer: %f", fahrenheit);
}
else if(answer == "FC"){
printf("Type the temperature here: ");
scanf("%lf", &temp);
printf("Answer: %f", celsius);
}
return 0;
}
calculator
You can't compare strings like this in C. There strcmp and strncmp functions. Besides this C strings are ended with \0 symbols, so your code should be something like this:
#include <stdio.h>
#include <string.h>
int main()
{
double temp, fahrenheit, celsius;
char answer[3];
printf("Type 'CF' if you want to convert from celsius to fahrenheit, and 'FC' if you want to convert from fahrenheit to celcius: ");
fgets(answer, 3, stdin);
fahrenheit = (temp * 1.8) + 32;
celsius = (temp - 32) * 0.5556;
if (strcmp(answer, "CF") == 0) {
printf("Type the temperature here: ");
scanf("%lf", &temp);
printf("Answer: %f", fahrenheit);
} else if (strcmp(answer, "FC") == 0){
printf("Type the temperature here: ");
scanf("%lf", &temp);
printf("Answer: %f", celsius);
}
return 0;
}
Use strcmp for
(answer == "CF"){
i.e.
strcmp(answer, "CF") == 0

Declaration of 'int a' shadows a parameter

I am new to c++ and trying to calculate Xk+1 = Xk - a*Xk*h for user entered values. Here's what I tried:
#include<stdio.h>
float fn_calculate(int x1, int x0, int a, float h)
{
int x;
int a;
float h;
int k;
for(k=-1; k<=100; k++);
{
float x1=x0-a*x0*h;
printf("please enter a 'x' value:\n");
scanf("%f",&x);
printf("please enter a 'a' value:\n");
scanf("%f",&a);
printf("please enter a 'h' value:\n");
scanf("%f",&h);
printf("the result is: %f\n", x1);
}
return 0;
}
You declare a inside the function. There's a parameter with the same name, a.
Doing that you cannot access the parameter a but only the local variable defined. You should change the name of either.
The same happens for h. You have to choose different names for both of them.
The right expression with main function is here:
#include<stdio.h>
float fn_calculate() {
float x0;
int k;
float h, a;
for (k = -1; k <= 100; k++);
{
printf("please enter a 'x' value:\n");
scanf("%f", &x0);
printf("please enter a 'a' value:\n");
scanf("%f", &a);
printf("please enter a 'h' value:\n");
scanf("%f", &h);
float x1 = x0 - a * x0 * h;
printf("the result is: %f\n", x1);
}
return 0;
}
int main(){
// you will calculate the formula below.
printf("x_{n+1} = x_{n} - a * x_{n} * h ; \n");
fn_calculate();
return 0;
}
By the way, note these problems below may be good for you:
Add some comments;
Add a main function for your program;
Learn about the parameter passing;
Learn about variable type, like %d for int and %f for float.

In C++ finding sinx value with Taylor's Series

I am trying to write a block of codes in C++ that calculates sinX value with Taylor's series.
#include <iostream>
using namespace std;
// exp example
#include <cstdio> // printf
#include <cmath> // exp
double toRadians(double angdeg) //convert to radians to degree
{ //x is in radians
const double PI = 3.14159265358979323846;
return angdeg / 180.0 * PI;
}
double fact(double x) //factorial function
{ //Simply calculates factorial for denominator
if(x==0 || x==1)
return 1;
else
x * fact(x - 1);
}
double mySin(double x) //mySin function
{
double sum = 0.0;
for(int i = 0; i < 9; i++)
{
double top = pow(-1, i) * pow(x, 2 * i + 1); //calculation for nominator
double bottom = fact(2 * i + 1); //calculation for denominator
sum = sum + top / bottom; //1 - x^2/2! + x^4/4! - x^6/6!
}
return sum;
}
int main()
{
double param = 45, result;
result = mySin(toRadians(param)); //This is my sin value
cout << "Here is my homemade sin : " << result << endl;
result = sin(param); //This is library value
cout << "Here is the API sin : " << result << endl;
return 0;
}
So my program works without any error. My output is exactly:
Here is my homemade sin : nan
Here is the API sin:0.850904
I know I am making a big logic mistake but I couldn't find it out. It is my second week with C++. I am more familiar with Java. I coded the same thing and It worked absolutely perfect. The answers matched each other.
Thanks for your time and attention!
in fact, you miss the return: x*fact(x-1); should be return x*fact(x-1);. You can see the compiler complaining if you turn the warnings on. For example, with GCC, calling g++ -Wall program.cpp gives Warning: control reaches end of non-void function for the factorial function.
The API sin also needs the angle in radians, so change result=sin(param); into result=sin(toRadians(param));. Generally, if in doubt about the API, consult the docs, like here.
Your codes seems to have some logical mistakes. Here is my corrected one:
#include <iostream>
using namespace std;
double radians(double degrees) // converts degrees to radians
{
double radians;
double const pi = 3.14159265358979323846;
radians = (pi/180)*degrees;
return radians;
}
double factorial(int x) //calculates the factorial
{
double fact = 1;
for(; x >= 1 ; x--)
{
fact = x * fact;
}
return fact;
}
double power(double x,double n) //calculates the power of x
{
double output = 1;
while(n>0)
{
output =( x*output);
n--;
}
return output;
}
float sin(double radians) //value of sine by Taylors series
{
double a,b,c;
float result = 0;
for(int y=0 ; y!=9 ; y++)
{
a= power(-1,y);
b= power(radians,(2*y)+1);
c= factorial((2*y)+1);
result = result+ (a*b)/c;
}
return result;
}
double n,output;
int main()
{
cout<<"enter the value\t";
cin>>n;
n = radians(n);
cout<< "\nthe value in radians is\t"<< n << "\n";
output = sin(n);
cout<< "\nsine of the given value is\t"<< output;
return 0;
}
The intention of this program was to use custom functions instead of libraries to make learning for others easy.
There are four user defined functions in this program.The first three user defined functions 'radians()', 'factorial()','power()', are apparently simple functions that perform operations as their name suggests.
The fourth function 'sin()' takes input in radians given by the function 'radians()'. The sin function uses Taylors series iterated term wise in the function's 'for(int y= 0;y!=9;y++)' loop till nine iterations to calculate the output.The 'for()' loop iterates the general mathematical expression: Term(n)=((-1)^n).(x^(2n+1))/(2n+1)!
sin(x)= x- x^3/3! + x^5/5! -x^7/7! + x^9/9!
=x-x^3/2*3 (1- x^2/4*5 + x^4/4*5*6*7 + x^6/4*5*6*7*8*9)
=x - x^3/2*3 {1- x^2/4*5(1- x^2/6*7 + x^4/6*7*8*9)}
=x - x^3/2*3 [{1- x^2/4*5 ( 1- x^2/6*7 (1- x^2/8*9))}]
=x(1 - x^2/2*3 [{1- x^2/4*5 ( 1- x^2/6*7 (1- x^2/8*9))}])
double sin_series_recursion(double x, int n){
static double r=1;
if(n>1){
r=1-((x*x*r)/(n*(n-1)));
return sin_series_recursion(x,n-2);
}else return r*x;
}

Declarations, Definitions and Calls

i need to gain a better understanding of function definition, declarations and proper calls using this program. I really need the understanding of how to use them. Could you show me the proper way to write this program with all three correct and explained?
#include <stdio.h>
#include <math.h>
quad_equation(float a, float b, float c);
int main()
{
float a, b, c, determinant, r1,r2;
printf("Enter coefficients a, b and c: ");
scanf("%f%f%f",&a,&b,&c);
determinant=b*b-4*a*c;
if (determinant>0)
{
r1= (-b+sqrt(determinant))/(2*a);
r2= (-b-sqrt(determinant))/(2*a);
printf("Roots are: %.2f and %.2f",r1 , r2);
}
else if (determinant==0) { r1 = r2 = -b/(2*a);
printf("Roots are: %.2f and %.2f", r1, r2);
}
else (determinant<0);
{
printf("Both roots are complex");
}
return 0;
I just solved this exact question here : (I guess this is a part of an assignment )
https://stackoverflow.com/a/19826495/1253932
Also looking at your code .. you never use the function quad equation .. also you haven't defined the type of the function ( int/void/float/char) etc.
For ease: ( here is the entire code ) -- ask me if you don't understand anything
#include <stdio.h>
#include <math.h>
// function declarations
void twoRoots (float a,float b,float delta);
void oneRoot (float a,float b,float delta);
int main (void)
{
//Local Declarations
float a;
float b;
float c;
float delta;
// float solution;
printf("Input coefficient a.\n");
scanf("%f", &a);
printf("Input coefficient b.\n");
scanf("%f", &b);
printf("Input coefficient c.\n");
scanf("%f", &c);
printf("%0.2fx^2 + %0.2fx + %0.2f\n", a, b, c);
delta = (float)(b*b) - (float)(4.0 * a * c);
printf("delta = %0.2f\n",delta);
if (delta > 0){
twoRoots(a,b,delta);
}else if (delta == 0) {
oneRoot(a,b,delta);
}else if (delta < 0.0){
printf("There are no real roots\n");
}
return 0;
}
void twoRoots (float a,float b,float delta)
{
float xOne;
float xTwo;
float deltaRoot;
printf("There are two distinct roots.\n");
deltaRoot = sqrt(delta);
xOne = (-b + deltaRoot) / (2*a);
xTwo = (-b - deltaRoot) / (2*a);
printf("%.2f", xOne);
printf("%.2f", xTwo);
}
void oneRoot(float a,float b,float delta)
{
float xOne;
// float xTwo;
// float deltaRoot;
printf("There is exactly one distinct root\n");
xOne = -b / (2*a);
printf("%.2f", xOne);
}
EDIT:
A slightly more optimized and better functioning code that I made from the above mentioned code:
http://pastebin.com/GS65PvH6
Edit2:
From your comments you try to do this:
printf("Enter coefficients a, b and c: ");
scanf("%f%f%f",&a,&b,&c);
This will fail if you input something like this: 121
Beacuse scanf will read the whole 121 into a and it will have nothing for b,c ( rather it will put \n(enter) into b and undefined into c )
So use the scanf the way I have used it in my code
OK - this is full of problems! I attempt to point them out, and show what "better" looks like. I hope this helps.
quad_equation(float a, float b, float c);
This is probably intended to be a "function prototype". A prototype tells the compiler "I am going to use this function later, and this is how it needs to be called, and the type it returns". You did not specify a return type; probably you want to use int to say whether you found roots or not, and print out the result in the function. Better would be to pass space for two return values as a parameter:
int quad_equation(float a, float b, float c, float* x1, float* x2);
Now we can use the main program to get input/output, and let the function solve the problem:
int main(void) {
{
float a, b, c, r1, r2;
int n;
// here you get the inputs; that seems OK
printf("Enter coefficients a, b and c: ");
scanf("%f %f %f",&a,&b,&c);
// now you have to "call your function"
// note that I make sure to follow the prototype: I assign the return value to an int
// and I pass five parameters: the coefficients a, b, c and the address of two variables
// x1 and x2. These addresses will be where the function puts the roots
n = quad_equation(a, b, c, &r1, &r2);
// when the function returns, I can print the results:
printf("There are %d roots:\n", n);
// based on the value of n, I change what I want to print out:
if (n == 2) printf(" %f and ", r1); // when there are two roots I print "root 1 and"
if (n > 0) printf("%f\n", r2); // when there is at least one root, I print it
// note that if n == 0, I would skip both print statements
// and all you would have gotten was "There are 0 roots" in the output
}
int quad_equation(float a, float b, float c, float* x1, float* x2) {
// function that computes roots of quadratic equation
// and returns result in x1 and x2
// it returns the number of roots as the return value of the function
float determinant;
determinant=b*b-4*a*c;
if (determinant>0)
{
*x1 = (-b+sqrt(determinant))/(2*a);
*x2= (-b-sqrt(determinant))/(2*a);
return 2;
}
if (determinant==0) {
*x1 = *x2 = -b/(2*a);
return 1;
}
return 0;
}

Recursive function returns unexpected result

My funciton takes a number input from the user and recursively sums the number 'n' to one.
Inputting a 5 would sum 1/5 + 1/4 + 1/3+ 1/2 + 1/1.
#include<stdio.h>
#include<conio.h>
//to
float recursion(float num,float sum);
void main(void)
{
float num=5,sum=0;
//input num
printf("%d",recursion(num,sum));
getch();
}
float recursion(float num,float sum)
{
// int sum=0; every time u run it the sum is assigned 0
if( num==1)
return 1;
else
{
sum=sum+(1/num);
num--;
recursion(num,sum);
}
return sum;
}//recursion function ends
The problem is, that it is giving 0. Can anyone help, please?
You should return the result of the recursive call:
return recursion(num,sum);
instead of return sum.
Why's the printf("%d") while it's supposed to print a float? Doesn't that display an integer making it always 0 for a float less than 0?
float recursion(float num)
{
if( num==1.0f)
{
printf("1/1 = ");
return 1.0f;
}
float inverse = 1.0f/num;
printf("1/%.0f + ", num);
return (inverse + recursion(--num));
}//recursion function ends
Here's the test code:
float num=5,sum=0;
float expected = 0;
for (int i = 1; i <= num; ++i)
{
expected += 1.0f/i;
}
//input num
printf("Expected %f and got %f",expected, recursion(num));
Output:
1/5 + 1/4 + 1/3 + 1/2 + 1/1 = Expected 2.283334 and got 2.283334
Hope this helps.
float recursion(float num) {
if( num==1)
return 1;
return (1.0/num) + recursion(num - 1);
}
By the way, do not input a negative number!
#fahad: Changes in your code has been commented in the code below:
float recursion2(float num,float sum)
{
// int sum=0; every time u run it the sum is assigned 0
if( num==1)
// Vite Falcon: Needs to return sum + 1
return sum + 1.0f;
else
{
// Vite Falcon: This is not really necessary.
//sum=sum+(1/num);
float inverse = 1.0f/num;
num--;
// Vite Falcon: The new sum is returned by the recursive function and so
// should be stored and returned.
sum = recursion2(num,sum + inverse);
}
return sum;
}//recursion function ends
PS: Sorry I had to answer again because I don't know how to add multi-line code as a comment.
Use sum=sum+(1.0/num);. When you divide 1, an integer with a float, the float gets converted to integer first.
float recursion(int num) {
if (num == 0) {
return 0;
}
return 1 / num + recursion(num--);
}