Why I can't pass a string as parameter of a function? - arduino

I'm writing a code and I can't use a String as parameter of a function, the Arduino keeps resetting. This is my original code thad do not works:
Serial.print(readLine("routes.txt", 1)); // calling the function
String readLine(String fileName, unsigned int lineNum)
{
if (!SD.exists(fileName))
{
Serial.println("- " + fileName + " do not exists!");
return ("FAILURE");
}
[continue the code...]
This code works:
Serial.print(readLine(1)); // calling the function
String readLine(unsigned int lineNum)
{
if (!SD.exists("routes.txt"))
{
Serial.println("- " + "routes.txt" + " do not exists!");
return ("FAILURE");
}
[continue the code...]
Anyone to help me?

Try assigning the value "routes.txt" into a variable of type String first and then use this variable as a parameter when calling the method.
The compiler might assume the value is a char-array instead of a String-value.

Related

Pulling data from Sqlite cursor loader

OK I have a app that has a Listview and uses sqlite data to populate it. But I also want to be able to email the contents of each view for the body of the message. I tried doing this with a global string variable and catching the data in the CarCursorAdapter activity in the BindView section like this:
// Update the TextViews with the attributes for the current move
vinTxtView.setText(vinStr);
dateTxtView.setText(dateStr);
mvFromTxtView.setText(mvFrom);
mvToTxtView.setText(mvTo);
MyProperties.getInstance().bodyStrGlobal = MyProperties.getInstance().bodyStrGlobal+vinStr+"-"+mvFrom+"->"+mvTo+"-"+dateStr+"\n";
And then I use that string in the email intent. But the problem is it keeps adding to this string every time the listview is populated so I get all kinds of double entries. What would be the best way to just capture this once when the email feature is selected? Or reset the string to null at some place? Maybe just read from each listview item instead of from the cursor loader? There is probably a way to just cycle through the database table but I'm getting all kinds of errors and haven't had any luck.
Found this to work. My MainActivity is very busy now, but everything works.
public String getEmailText(){
String tempStr = "";
String[] projection = {
CarEntry._ID,
CarEntry.COLUMN_CAR_VIN,
CarEntry.COLUMN_CAR_DATE,
CarEntry.COLUMN_CAR_MOVEFROM,
CarEntry.COLUMN_CAR_MOVETO};
Cursor cursor = getContentResolver().query(Uri.parse("content://gregorykraft.com.scanvin/cars"),projection,null,null,null);
if
(cursor == null || cursor.getCount() < 1) {
Toast.makeText(this, getString(R.string.error_uri),
Toast.LENGTH_SHORT).show();
return "";
}
int i = 0;
if (cursor.moveToFirst()) {
while (!cursor.isAfterLast()) {
i++;
String s =String.valueOf(i);
String vinStr = cursor.getString(cursor.getColumnIndex(CarEntry.COLUMN_CAR_VIN));
String mvFrom = cursor.getString(cursor.getColumnIndex(CarEntry.COLUMN_CAR_MOVEFROM));
String mvTo = cursor.getString(cursor.getColumnIndex(CarEntry.COLUMN_CAR_MOVETO));
String dateStr = cursor.getString(cursor.getColumnIndex(CarEntry.COLUMN_CAR_DATE));
tempStr = tempStr+s+") "+vinStr + "-" + mvFrom + "->" + mvTo + "-" + dateStr + "\n";
cursor.moveToNext();
}
cursor.close();
}
return tempStr;
}

How do I know whether FileInputStream opens a file?

I am using Poco::FileInputStream to design a copy function
void do_copy_file(Poco::FileInputStream & iss)
{
Poco::FileOutputStream fos("output.txt");
Poco::StreamCopier::copyStream(iss, fos);
}
Then, a user can call do_copy_file like this
Poco::FileInputStream fis;
do_copy_file(fis);
My question: Can I judge whether iss refers a valid file?
The Poco::FileOutputStream just throws a Poco::FileException if an error occurs when trying to open it, e.g. if a invalid file path is used. It doesn't have any function to test whether it is valid.
What you could do is change your do_copy_file() function to catch an Poco::FileException exception and return a boolean value - true if opened successfully or false otherwise:
bool do_copy_file(Poco::FileInputStream & iss)
{
bool result(true);
try
{
Poco::FileOutputStream fos("output.txt");
Poco::StreamCopier::copyStream(iss, fos);
}
catch (const Poco::FileException&)
{
result = false;
}
return result;
}
Then you call it like this:
Poco::FileInputStream fis;
if (do_copy_file(fis)
{
//output file stream opened successfully
}
If you want do_copy_file() to catch an exception for opening the input stream I would recommend doing that in the function itself. Instead of passing the input streams pass the file paths instead:
bool do_copy_file(const std::string &inputFile, const std::string& outputFile)
{
bool result(true);
try
{
Poco::FileInputStream fis(inputFile);
Poco::FileOutputStream fos(outputFile);
Poco::StreamCopier::copyStream(fis, fos);
}
catch (const Poco::FileException&)
{
result = false;
}
return result;
}

"Not an allowed type" in Turbo C++

I'm trying to make a grading system.So what i want to do is take 1/3 of the value of outputgrade and add it with 2/3 of the value of outputgrade2, I tried midterm1=(outputgrade()*1/3)+(outputgrade2*2/3) but I receive an error which is
Not an allowed type
Please somebody help me on what to do.
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
double AAO,Quizzes,Project,MajorExam,Midterm;
void inputprelim()
{
clrscr();
gotoxy(3,4);cout<<"Input Prelim Grade";
gotoxy(1,6);cout<<"Attendance/Ass./Oral: ";cin>>AAO;
gotoxy(1,7);cout<<"Project: ";cin>>Project;
gotoxy(1,8);cout<<"Quizzes: ";cin>>Quizzes;
gotoxy(1,9);cout<<"Major Exam: ";cin>>MajorExam;
gotoxy(1,11);cout<<"Prelim Grade: ";
}
int getgrade(double a, double b, double c, double d)
{
double result;
result=(a*0.10)+(b*0.20)+(c*0.30)+(d*0.40);
cout<<setprecision(1)<<result;
return result;
}
void outputgrade()
{
getgrade(AAO,Project,Quizzes,MajorExam);
getch();
}
void inputmidterm()
{
gotoxy(33,4);cout<<"Input Midterm Grade";
gotoxy(29,6);cout<<"Attendance/Ass./Oral: ";cin>>AAO;
gotoxy(29,7);cout<<"Project: ";cin>>Project;
gotoxy(29,8);cout<<"Quizzes: ";cin>>Quizzes;
gotoxy(29,9);cout<<"Major Exam: ";cin>>MajorExam;
gotoxy(29,11);cout<<"Temporary Midterm Grade: ";
gotoxy(29,12);cout<<"Final Midterm Grade: ";
}
void outputgrade2()
{
getgrade(AAO,Project,Quizzes,MajorExam);
getch();
}
void main()
{
inputprelim();
gotoxy(15,11);outputgrade();
inputmidterm();
gotoxy(54,11);outputgrade2();
gotoxy(55,11);
Midterm1=(outputgrade()*1/3)+(outputgrade2()*2/3);
}
Your functions outputgrade() and outputgrade2() are of return type void.
In order to use them in midterm1=(outputgrade()*1/3)+(outputgrade2*2/3) you need to change their return type as int/double/float,etc.
If I have understood the logic of your code correctly then edit the two functions as:
double outputgrade()
{
return getgrade(AAO,Project,Quizzes,MajorExam);
}
double outputgrade2()
{
return getgrade(AAO,Project,Quizzes,MajorExam);
}
What I have done is changed the return type to double and at the same time the two functions are now returning whatever value getgrade returns.
outputgrade() and outputgrade2() need to return a numeric value for use in the calculation.
Right now they don't return anything.

how can i use sqlite rawQuery?

when I call select in other function, there is problem in line number 3.
Is it wrong?
public String[] select(int n){
db = helper.getReadableDatabase();
Cursor c = db.rawQuery("SELECT * FROM info WHERE number='" + n + "'", null);
}
The rawQuery() looks good, though usually you wouldn't quote integers as 'string literal'.
However, a non-void method must return a value and your method doesn't return anything. Add return null; to make it compile; implement a loop that builds a string array to return a non-null value.

how to change "IHtmlString" to "MvcHtmlString"?

Currently I'm writing 2 helper method to extend a implementation where I'm using "IHtmlString", how I can convert this to one method by using "MvcHtmlString"? Help...
public static IHtmlString ExceptionValidationSummary(this HtmlHelper helper)
{
const string template = "<div class=\"ui-widget\"><div class=\"ui-state-error ui-corner-all\" style=\"padding:0 .7em\"><div>" +
"<span class=\"ui-icon ui-icon-alert\" style=\"float: left; margin-right: .3em;\"></span>" +
"<strong>Validation Exceptions:</strong></div><div style=\"margin-top: 5px;\"> " +
"<ul style=\"font-weight: normal;\">{0}</ul></div></div></div>";
StringBuilder exceptionList = new StringBuilder();
// Iterate through the exceptions
foreach (var error in helper.ViewData.ModelState.SelectMany(modelState => modelState.Value.Errors))
{
exceptionList.Append(string.Format("<li>{0}</li>", error.ErrorMessage));
}
return exceptionList.Length.Equals(0) ? string.Format("").Raw() : string.Format(template, exceptionList).Raw();
}
public static IHtmlString Raw(this string value)
{
return new HtmlString(value);
}
Though I'd expect the StringBuilder.ToString() method to be implicitly called by string.Format(), if that is not happening, explicitly call the ToString() method like this:
string.Format(template, exceptionList.ToString())
Also, your method is declared to return an IHtmlString. If you change the signature to use MvcHtmlString, that will tell the compiler your desired return type. At this point, it is just a matter of ensuring the return value matches the updated declaration:
return MvcHtmlString.Create(string.Format(template, exceptionList.ToString()));

Resources