I have a simple test program in C++ that prints out attributes of a circle
#include <iostream>
#include <stdlib.h>
#include "circle.h" // contains the Circle class
using namespace std;
void print_circle_attributes(float r) {
Circle* c = new Circle(r);
cout << "radius: " << c->get_radius() << endl;
cout << "diameter: " << c->get_diameter() << endl;
cout << "area: " << c->get_area() << endl;
cout << "circumference: " << c->get_circumference() << endl;
cout << endl;
delete c;
}
int main(int argc, const char* argv[]) {
float input = atof(argv[0]);
print_circle_attributes(input);
return 0;
}
when I run my program with the parameter 2.4 it outputs:
radius: 0.0
diameter: 0.0
area: 0.0
circumference: 0.0
I've previously tested the program without the parameter, but simply using static values, and it ran just fine; so I know there's nothing wrong with the class I made...
So what did I do wrong here?
argv[0] is the program name. You want argv[1] for the first argument.
Also, check that argc is at least two before trying to access it. You might also consider std::stoi, std::istringstream or strtod rather than atoi for conversion, since they can detect bogus input.
Finally, why are using new when an automatic variable will suffice? You should get out of that habit straight away, or spend the rest of eternity debugging memory leaks.
argv[0] is the name of the executable being invoked.
Your first command line parameter will be in argv[1].
To make sure that your program does not silently fail again, you should check how many parameters you actually have and if the atof returns a value, and show a message to the user explaining the issue accordingly.
Related
I am new in VS code. I wrote a C++ code like one below. but unfortunately in the terminal or output panel I cannot get both of the string and variable value. in the terminal only variable's inputted value is showing. How to fix this?
#include <bits/stdc++.h>
int main()
{
int slices;
std::cin >> slices;
std::cout << "You got " << slices << " of pizzas" << std::endl;
return 0;
}
I am a very novice programmer, and I am trying to understand the find functions for strings. At uni we are told to use c-strings, which is why I think that it isn't working. The problem comes when I compile, there is a compile error that line was not declared. This is my code:
#include <iostream>
#include <fstream>
#include <cstring>
#include <string>
using namespace std;
int main()
{
char test[256];
char ID[256];
cout << "\nenter ID: ";
cin.getline(ID, 256);
int index = line.find(ID);
cout << index << endl;
return 0;
}
Please help, it has become really frustrating as I need to understand this function to complete my assignment :/
You're trying to use C-style strings. But find is a member of the C++ string class. If you want to use C-style strings, use functions that operate on C style strings like strcmp, strchr, strstr, and so on.
Supposing you actually input some data into test also, then one way to do it would be:
char *found = strstr(test, ID);
if ( !found )
cout << "The ID was not found.\n";
else
cout << "The index was " << (found - test) << '\n';
Because find fuction a member function string class,You should declare a string class's object. I think you will do that like this:
string test = "This is test string";
string::size_type position;
position = test.find(ID);
if (position != test.npos){
cout << "Found: " << position << endl;
}
else{
cout << "not found ID << endl;
}
I have tried to adapt my knowledge of modularity to Visual C++ however, upon what seems to be an endless search scouring for syntax, I simply can't get this right. Basically in this code, the menu is called first, once the user enters their choice (only coded option 1 thus far) to return that value to the main, which then steps into the if statement and calls fahrenheit. I am requesting the syntax for passing by reference, I know C#'s syntax for this, but not Visual C++
Here's the code.
// Test.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
void Celsius()
{
}
void fahrenheit()
{
cout << "Success!" << endl; //....Outputs this just to see if the module is being called properly.
}
int menu(int Mystring) //....I was testing this syntax to pass the variable.
{
cout << "What would you like to do : " << endl;
cout << "1) Fanreheit to Celsius" << endl;
cout << "2) Celsius to Fahrenheit" << endl;
cout << "Choice : " ;
cin >> Mystring;
return Mystring;
}
int main()
{
int celsius = 0;
int fahrenheit = 0;
int Mystring = 0;
menu(Mystring); //....Testing this syntax to pass Mystring to menu.
if (Mystring == 1) //....I was hoping the menu would return Mystring as value = 1.
{
fahrenheit(); //.......I want this to call fahrenheit module if Mystring = 1
}
}
The "things" you're talking about aren't called modules, but functions. That's a pretty big difference and I think you should know it, since you won't understand nearly any article without that knowledge.
That being cleared, the problem in your code is, that you pass the variable by value (int menu(int Mystring)), while - in order to change it inside the function - you need to pass it by reference or pointer:
int menu(int &Mystring)
There are plenty of articles about functions in C++. You should check them out probably.
The arrays are passed by reference. Any changes made to the array within the function changeArray will be observed in the calling scope (main function here).
However the codes below print 0 1 in the 1st cout, and print 2 in the 2nd "cout". What I don't understand is that why the first cout prints the original value of array[0]=1 instead of the changed value of array[0]=2?
Thanks a lot.
#include <iostream>
using namespace std;
int changeArray(int array[]) {
array[0]=2*array[0];
return 0;
}
int main() {
int array[]={1,2,3,4};
cout << changeArray(array) << " " << array[0] << endl;
cout << array[0] << endl;
return 0;
}
To make sure that the compiler doesn't reorder the execution:
cout << array[0] << endl;
changeArray(array);
cout << array[0] << endl;
This prints 1 and then 2.
The C++ compiler is allowed to optimize the code by reordering the execution of code within a single expression (e.g. cout << changeArray(array) << " " << array[0] << endl). To avoid that, and to make sure changeArray gets called first, you need to split your expression to separate statements, e.g. by using the semicolon (;). Everything before the semicolon gets executed before anything after the semicolon can start.
The following code has overloaded function CandyBarFunc. First prototype defines the function so that it modifies the value of a structure. Second prototype defines the function so that it just displays the content of a passed structure. The problem is that when I run the console program nothing appears on the screen except the Press Any Key...
I tried to debug it and found out that first prototype works properly(I added the display functionality from the second prototype to the first one) becuase it modified and displayed the contents of the structure. So therefore it seems that overloading didn't work because the second function prototype doesn't get called during execution because nothing is displayed on the console screen. I'm not sure if the signaure is bad because the compiler does't complain about the ambigious function call. Did I miss something obvious in the code?
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
struct CandyBar
{
char name[40];
double weight;
int calories;
};
void CandyBarFunc(CandyBar & astruct, const char * aname = "Millennium Munch", double aweight = 2.85, int acalories = 350);
void CandyBarFunc(const CandyBar & astruct);
int main(void)
{
CandyBar MyCandyBar =
{
"Hi",
1.5,
456
};
cout << "1" << endl; 'little debug'
CandyBarFunc(MyCandyBar); 'suppose to display the contents of MyCandyBar'
CandyBarFunc(MyCandyBar, "Hello World Candy Bar", 1.25, 200); 'suppose to modify MyCandyBar
CandyBarFunc(MyCandyBar); 'suppose to display the contents of MyCandyBar again'
cout << "2"; 'little debug'
return 0;
}
void CandyBarFunc(CandyBar & astruct, const char * aname, double aweight, int acalories)
{
strncpy_s(astruct.name,aname,40);
astruct.weight = aweight;
astruct.calories = acalories;
cout << "Name: " << astruct.name << endl; 'not suppose to be here, just for debug'
cout << "Weight: " << astruct.weight << endl; 'not suppose to be here, just for _ debug'
cout << "Calories: " << astruct.calories; 'not suppose to be here, just for debug'
}
void CandyBarFunc(const CandyBar & astruct)
{
cout << "Name: " << astruct.name << endl;
cout << "Weight: " << astruct.weight << endl;
cout << "Calories: " << astruct.calories;
}
Exercise:
The CandyBar structure contains three members. The first member holds the brand
name of a candy bar. The second member holds the weight (which may have a fractional
part) of the candy bar, and the third member holds the number of calories (an integer
value) in the candy bar. Write a program that uses a function that takes as arguments a
reference to CandyBar, a pointer-to-char, a double, and an int and uses the last three
values to set the corresponding members of the structure. The last three arguments
should have default values of “Millennium Munch,” 2.85, and 350. Also, the program
should use a function that takes a reference to a CandyBar as an argument and displays
the contents of the structure. Use const where appropriate.
Since MyCandyBar isn't const, the compiler choses the first (reference to non-const) overload.
But seriously, if you want one function to set properties and another function to print them out, please don't abuse overloading by giving them the same name. Just name them differently, no more problems.
Also, in C++ we prefer std::string to fixed-size character arrays and character pointers.
Since MyCandyBar is not const, it will always try to use the function which accepts the non const CandyBar. You can force it to call the other function by casting it to const:
CandyBarFunc((const CandyBar &)MyCandyBar);