syntax problem

Status
Not open for further replies.

aaronkupen

Fully Optimized
Messages
1,848
Location
Pittsburgh, PA
Code:
#include <stdio.h>
#include <iostream.h>
#include <stdlib.h>
#include <windows.h>

String information()
{
       String sName;
       cout << "Enter your name";
       cin >> sName;

       return sName;
}

int main(int nNumberofArgs, char* pszArgs[])
{

    double dAccumulator;
        do
             {
             cout << "Enter a digit to be added to the accumulator, if you get the accumulator to zero the program will exit";
             double dInput;
             cin >> dInput;

             String sInfo;
             dAccumulator = dAccumulator + dInput;
             sInfo = information();
             cout << sInfo;
             cout << "'s accumulator is now ";
             cout << dAccumulator;
             }
         while (dAccumulator != 0);

    system("PAUSE");
    return 0;
}
Im getting errors with this program, and the only thing I can think of is that im declaring a string variable wrong, it doesnt worth with a capital S or a lower case one. Can you even make a string variable? Any help would be appreciated.
 
string would be lower case

inoder to use string you must include #include<string> well that the newer version since you seem to be using the older versions of headers it would either be #include<string.h> or #include<cstring.h> dont remember which.
 
double dAccumulator;

That is the source of your problem, you need to initialize it with a value, in your case 0 would be appropriate.

dAccumulator = dAccumulator + dInput; //Here is the actual bad code

junk = junk + dInput; //See it now
 
alright, those problems helped, but i am still getting errors, and also when I initialize an int variable, do I also have to set a value to it?
 
Let us see what the errors are saying, i'm too lazy to compile your code myself. You only need to initialize the variable if you are using it like you did.

If i say int time, and then input a value from the user and then use it in a calculation i don't have to initialize it because it was initialized by the user before the calculation. But if i am using it in the actual calculation before it has been assigned a value you don't know what is inside it so it will bum out.

string with a lower case s as well, using namespace std should be there and just use int main().
 
Code:
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;


string information();

int main()
{

    double dAccumulator = 0;
        do
             {
             cout << "\nEnter a digit to be added to the accumulator, if you get the accumulator to zero the program will exit: ";
             double dInput;
             cin >> dInput;

             string sInfo;
             dAccumulator = dAccumulator + dInput;
             sInfo = information();
             cout << sInfo<<endl;
             cout << "'s accumulator is now ";
             cout << dAccumulator;
             }
         while (dAccumulator != 0);

    system("PAUSE");
    return 0;
}

string information()
{
       string sName;
       cout << "\nEnter your name: ";
       cin >> sName;

       return sName;
}

Thats a little better, you should clean it up a bit.
 
Thanks, I understand what you are saying. Also the errors im getting are : line 13- syntax error before {, line 16 syntax error before <, line 17- syntax error before >, line 31 - 'string' undeclared [first use this function]. Those are a few, but im guessing one problem is causing all of these and the rest. (the lines im using are from the original syntax i sent with the information function first)
 
Status
Not open for further replies.
Back
Top Bottom