lotrsimp12345 37 Posting Pro in Training

actually i don't think i need that +1 because it starts counting at 0.
So if i entered in "ab" mycstring[0]=a mycstring[1]=b mycstring[2]='\0'.

lotrsimp12345 37 Posting Pro in Training

most recent code

//main

#include <iostream>
#include <cstdlib>
using namespace std;
#include "strextra.h"

int main()
{
    Search a;
    cout<<"enter max number of characters in the sentence";
    int number;
    cin>>number;
    cout<<"enter a sentence";
    char let;
    char mycstring[number+1];
    for(int i=0; i<number; i++)
    {
        cin>>let;
        mycstring[i]=let;
    }
     mycstring[number+1]='\0';
     cout<<"my string is\n"<<mycstring<<"\n";
    cout<<"enter the character you are trying to find in the string above\n";
    char alpha;
    
    char lett[2];
    for(int a=0; a<1; a++)
    {
        cin>>alpha;
        lett[a]=alpha;
    }
    lett[1]='\0';
    cout<<lett[0]<<lett[1];
    //a.find(mycstring, lett);
    
}

//interface

#ifndef STREXTRA_H_INCLUDED
#define STREXTRA_H_INCLUDED
#include <iostream>

using namespace std;

class Search
{
    public:
    int find(char mystring,char letter);
    int find(char mystring,string word);
    
};

//implementation

#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;
#include "strextra.h"

int search::find(char mystring,char letter)
{
    return strchr(mystring,letter);
}

#endif // STREXTRA_H_INCLUDED

lotrsimp12345 37 Posting Pro in Training

that's what i have the +1 for.

lotrsimp12345 37 Posting Pro in Training

nvm. It gives me some conversion error.

lotrsimp12345 37 Posting Pro in Training

it compiled.

lotrsimp12345 37 Posting Pro in Training

a c-string start's storing at 0 and then when i have lett[1] it stores the '\0' so it only stores one character correct?

lotrsimp12345 37 Posting Pro in Training

yeap i am compiling all of my files.

lotrsimp12345 37 Posting Pro in Training

i haven't learnt about pointers yet. That's the next section. He says to use c-string and overloading

lotrsimp12345 37 Posting Pro in Training

i am guessing the problem is that the parameters and arguments are matching up.

lotrsimp12345 37 Posting Pro in Training

thanks for the help now it says undefined reference to search::find(char*,char*). does that mean my return statement doesn't match up?

lotrsimp12345 37 Posting Pro in Training

i am supposed to make 2 functions called find and they are supposed to find a character or string if they are similar

when i do this progam gives me weird error:
"use of search ambigious"
"first declared as class search here.
//main

#include <iostream>
#include <cstdlib>
using namespace std;
#include "strextra.h"

int main()
{
    search a;
    cout<<"enter max number of characters in the sentence";
    int number;
    cin>>number;
    cout<<"enter a sentence";
    char let;
    char mycstring[number];
    for(int i=0; i<number; i++)
    {
        cin>>let;
        mycstring[i]=let;
    }
     mycstring[number]='\0';
     cout<<"my string is\n"<<mycstring<<"\n";
    cout<<"enter the character you are trying to find in the string above\n";
    char alpha;
    
    char lett[1];
    for(int a=0; a<1; a++)
    {
        cin>>alpha;
        lett[a]=alpha;
    }
    lett[1]='\0';
    a.find(mycstring, lett);
    
}

//interface

#ifndef STREXTRA_H_INCLUDED
#define STREXTRA_H_INCLUDED
#include <iostream>

using namespace std;

class search
{
    public:
    int find(char mystring[],char letter[]);
    int find(char mystring,string word);
    
};

#endif // STREXTRA_H_INCLUDED

//implementation

#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;
#include "strextra.h"

int search::find(char mystring[],char letter[])
{
    return strcspm(mystring[],letter[]);
}
lotrsimp12345 37 Posting Pro in Training

thanks i completely forgot about that.

lotrsimp12345 37 Posting Pro in Training

the error is on line 32 of my main program

//interface file

#ifndef STREXTRA_H_INCLUDED
#define STREXTRA_H_INCLUDED
#include <iostream>

using namespace std;

class search
{
    public:
    int find(unsigned char mystring,char letter);
    int find(char mystring[],string word);
    
};

#endif // STREXTRA_H_INCLUDED

//implementation

#include <iostream>
#include <cstdlib>

using namespace std;
#include "strextra.h"

int search::find(unsigned char mystring,char letter)
{
    return=strcspm(mystring,letter);
}

//main program

#include <iostream>
#include <cstdlib>
using namespace std;
#include "strextra.h"


int main()
{
    cout<<"enter max number of characters in the sentence";
    int number;
    cin>>number;
    cout<<"enter a sentence";
    char let;
    char mycstring[number];
    for(int i=0; i<number; i++)
    {
        cin>>let;
        mycstring[i]=let;
    }
     mycstring[number]='\0';
     cout<<"my string is\n"<<mycstring<<"\n";
    cout<<"enter the character you are trying to find in the string above\n";
    char alpha;
    
    char lett[1];
    for(int a=0; a<1; a++)
    {
        cin>>alpha;
        lett[a]=alpha;
    }
    lett[1]='\0';
    find(unsigned mycstring, lett);
    
}
lotrsimp12345 37 Posting Pro in Training

thanks for help.

lotrsimp12345 37 Posting Pro in Training

Char to string:
1
2
3
4
5
6
7


#include <sstream>
#include <string>
stringstream ss;
string s;
char c = 'a';
ss << c;
ss >> s;

String to int:
1
2
3
4
5
6
7


#include <sstream>
#include <string>
stringstream ss;
string s;
int n = 1;
ss << n;
ss >> s;

i think this would work.

lotrsimp12345 37 Posting Pro in Training

i think that using stringstream might work.

lotrsimp12345 37 Posting Pro in Training

they enter a problem set name and number. I am supposed to get in my problem name which i can do fine. for the numbers though i need to have it print out all the numbers they are supposed to do.

Example
input:
L1,2,3-5
output:
do problems 1,2,3,4,5 of L.

lotrsimp12345 37 Posting Pro in Training

i am supposed to this without pointers. The next topic is pointers.

lotrsimp12345 37 Posting Pro in Training

b=atoi(num,num.c_str());

it gives me error that request for member'c_str' in'num', which is of non-class type 'char'

what does that mean, i am passsing in a character

lotrsimp12345 37 Posting Pro in Training

still confused why it won't convert.

lotrsimp12345 37 Posting Pro in Training

k thanks.

lotrsimp12345 37 Posting Pro in Training

this is code that won't convert.

int b=atoi(cin.get(num));

lotrsimp12345 37 Posting Pro in Training

its in the num=="-".

#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{


    cout <<"enter the problemset and number""\n";
    //problems represents name and numbers
    string problems;
    char quote;
    char num;
    string number;
    
    //gather name
    if(cin.peek()=='"' || cin.peek() == '\'')
    {
        cin >>quote;
        getline(cin,problems,quote);
    }
    else
    {
        while (!isdigit(cin.peek()) && !isspace(cin.peek()))
        {
            (char)cin.peek();
            problems += cin.get();
        }
    }


    //gather problem numbers
    //cin >> num;
    int lastdigit=0;
    while(num != '\n'){
        
        cin.get(num);
        // can't use cin >> ws since it would eat the newline
       /* while (cin.peek() != '\n' && isspace(cin.peek()))
        {
            cin.ignore();
        }*/
        if(num==',')
        {
            if(isdigit(cin.peek()))
            {
                cin.get(num);
                cout<<"from comma"<<number<<"\n";
            }
                        
        }
        else if (num=='-')
        {
            if(isdigit(cin.peek()))
            {
            // remember num
             
            int b=atoi(cin.get(num));
            for(int a=lastdigit+1; a<=b; a++)
            {
                num++;  
            }
        }
        else if(cin.peek()=='\n')// must be a newline!
        {
//          
            break;
        }
        else if(isdigit(cin.peek()))
        {

        }
        if(isdigit(num))
        {
        lastdigit=num;
        }
        number=number+num;
    }
    
    cout<<"the final number is"<<number<<"\n"; 
        
    // print them all out

    return 0;
}
lotrsimp12345 37 Posting Pro in Training

nvm. going to ask teacher.

lotrsimp12345 37 Posting Pro in Training

so it works when there are quotes. but not when there aren't

lotrsimp12345 37 Posting Pro in Training

i mean quotes and single quotes.

lotrsimp12345 37 Posting Pro in Training
#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{


    cout <<"enter the problemset and number""\n";
    //problems represents name and numbers
    string problems , probtitle;
    string xx;
    bool x=true;
    char quote;
    string str1, str2;
    string begin;
    //gather name
    if(cin.peek()=='"' || cin.peek() == '\'')
    {
        cin >>quote;
        getline(cin,problems,quote);
    }
    else
    {
            getline(cin,problems);
    }

    //gather problem numbers

    while(x==true)
    {
        if(cin.peek()=='0'||cin.peek()=='1'||cin.peek()=='2'||cin.peek()=='3'||cin.peek()=='4'||cin.peek()=='5'||cin.peek()=='6'||cin.peek()=='7'||cin.peek()=='8'||cin.peek()=='9'||cin.peek()==','||cin.peek()=='-')
        {
            getline(cin,problems);
        }
        else
        {
            getline(cin,xx);
            if(xx.compare("\n"))
            {
            x=false;
            }
        }
    }
    cout<<problems;


int length=problems.find(',');
do{
    if(problems.find(',') != string::npos)
    {
        size_t a,b;
        a=problems.find(',');
        b=problems.find('-');


         if(problems.find('-') != string::npos&&b<a)
         {
                    str1 = problems.substr(0,problems.find('-'));
                    cout<<"from do ifif1\n"<<str1;
                    problems=problems.erase(0,problems.find('-'));
                    cout<<"from erase after first erase"<<problems;
                    str2=problems.substr(problems.find('-')+1,problems.find(','));
                    cout<<"from doifif2\n"<<str2;
                    problems=problems.erase(0,problems.find(',')+1);
                    cout<<"the problems left after erase are"<<problems;

        }
        else
        {
                    cout << "from do if else\n" ;
                    str1 = problems.substr(0,problems.find(','));
                    cout<<"from elseelseelseelse\n"<<str1;
                    problems=problems.erase(0,problems.find(',')+1);
        }

    }
    else
    {
        cout <<"No Comma\n";


        if(problems.find('-') != string::npos)
        {
                    str1 = problems.substr(0,problems.find('-'));
                    cout<<str1;
                    str2=problems.substr(problems.find('-')+1);
                    cout<<str2;
                    problems=problems.erase(0,problems.find('-')+1);
        }
        else
        {cout << "\n IF ELSE" ;
                    str1 = problems.substr(0,problems.find(','));
                    cout<<str1;
                    if(problems.find(',') != string::npos)
                    {
                    problems=problems.erase(0,problems.find(',')+1);
                    }
                    else{
                        problems="";
                    }
        }

    }
}while(problems.length()>0 );
lotrsimp12345 37 Posting Pro in Training

most recent code:

#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{


    cout <<"enter the problemset and number""\n";
    //problems represents name and numbers
    string problems , probtitle;
    string xx;
    bool x=true;
    char quote;
    string str1, str2;
    string begin;
    //gather name
    if(cin.peek()=='"' || cin.peek() == '\'')
    {
        cin >>quote;
        getline(cin,problems,quote);
    }
    else
    {
            getline(cin,problems);
    }

    //gather problem numbers

    while(x==true)
    {
        if(cin.peek()=='0'||cin.peek()=='1'||cin.peek()=='2'||cin.peek()=='3'||cin.peek()=='4'||cin.peek()=='5'||cin.peek()=='6'||cin.peek()=='7'||cin.peek()=='8'||cin.peek()=='9'||cin.peek()==','||cin.peek()=='-')
        {
            getline(cin,problems);
        }
        else
        {
            getline(cin,xx);
            if(xx.compare("\n"))
            {
            x=false;
            }
        }
    }
    cout<<problems;


int length=problems.find(',');
do{
    if(problems.find(',') != string::npos)
    {
        size_t a,b;
        a=problems.find(',');
        b=problems.find('-');


         if(problems.find('-') != string::npos&&b<a)
         {
                    str1 = problems.substr(0,problems.find('-'));
                    cout<<"from do ifif1\n"<<str1;
                    problems=problems.erase(0,problems.find('-'));
                    cout<<"from erase after first erase"<<problems;
                    str2=problems.substr(problems.find('-')+1,problems.find(','));
                    cout<<"from doifif2\n"<<str2;
                    problems=problems.erase(0,problems.find(',')+1);
                    cout<<"the problems left after erase are"<<problems;

        }
        else
        {
                    cout << "from do if else\n" ;
                    str1 = problems.substr(0,problems.find(','));
                    cout<<"from elseelseelseelse\n"<<str1;
                    problems=problems.erase(0,problems.find(',')+1);
        }

    }
    else
    {
        cout <<"No Comma\n";


        if(problems.find('-') != string::npos)
        {
                    str1 = problems.substr(0,problems.find('-'));
                    cout<<str1;
                    str2=problems.substr(problems.find('-')+1);
                    cout<<str2;
                    problems=problems.erase(0,problems.find('-')+1);
        }
        else
        {cout << "\n IF ELSE" ;
                    str1 = problems.substr(0,problems.find(','));
                    cout<<str1;
                    if(problems.find(',') != string::npos)
                    {
                    problems=problems.erase(0,problems.find(',')+1);
                    }
                    else{
                        problems="";
                    }
        }

    }
}while(problems.length()>0 );
lotrsimp12345 37 Posting Pro in Training

so input could be
"bob"1-5,6-10,11-15,57
or
'bob' 1-6,1-7,9-10
or
bob 1-7,12-18
spaces don't matter after problemset name
output could be
do problems 1,2,3,4,5,6,7,12,13,14,15,16,17,18 of bob. from last example

lotrsimp12345 37 Posting Pro in Training

what class is utility function from? can only use string class and c-string class.

lotrsimp12345 37 Posting Pro in Training

so my problem is they enter a line of code which contains the problemname which may contain quotes or not.Then have to find the numbers. Say do problems what ever numbers each seperated by a comma and finally state what problem set from. Can't have duplicate numbers and must be ordered.

lotrsimp12345 37 Posting Pro in Training

if it contains quote it cuts it out, then i am stuck on part when i just enter in a problemset without a quote or single quote. Then it doesn't gather numbers correctly but stores the entire string

int main()
{


    cout <<"enter the problemset and number""\n";
    //problems represents name and numbers
    string problems , probtitle;
    string xx;
    bool x=true;
    char quote;
    string str1, str2;
    string begin;
    //gather name
    if(cin.peek()=='"' || cin.peek() == '\'')
    {
        cin >>quote;
        getline(cin,problems,quote);
    }
    else
    {
            getline(cin,problems);
    }
cout<<problems;

    //gather problem numbers

    while(x==true)
    {
        if(cin.peek()=='0'||cin.peek()=='1'||cin.peek()=='2'||cin.peek()=='3'||cin.peek()=='4'||cin.peek()=='5'
||cin.peek()=='6'||cin.peek()=='7'||cin.peek()=='8'||cin.peek()=='9'||cin.peek()==','||cin.peek()=='-')
        {
            getline(cin,problems);
        }
        else
        {
            getline(cin,xx);
            if(xx.compare("\n"))
            {
            x=false;
            }
        }
    }
    cout<<problems;
lotrsimp12345 37 Posting Pro in Training

thanks that was really helpful.

lotrsimp12345 37 Posting Pro in Training

my problem is when it prompts again. it never goes and asks the user to hit enter. because once they hit enter it stops.

lotrsimp12345 37 Posting Pro in Training

so after you hit yes, it should print out original time and then wait for user to hit enter,then enter loop

lotrsimp12345 37 Posting Pro in Training
#include <iostream>
using namespace std;
#include "Interface.h"
#include <conio.h>




int main()
{
    apple t;

    bool quit = false;


    while(! quit)
    {
        cout<<"the original value at beginning of clock is\n"<<clock()<<"\n";
        t.start();
        cout<<"hit enter when you want to stop timer\n";
        if(cin.get()=='\n')
        {
            t.end();
            int time =t.elapsed();
            cout<<"the time difference is\n"<<time<<"\n";
            cout<<"enter yes if you want to find out time differences for more times\n";
            if(cin.get()=='y')
            {
                cout<<"from yes";
            }
            else
            {
                cout<<"from no";
                quit=false;
                break;
            }
        }
    }
    return 0;
}
lotrsimp12345 37 Posting Pro in Training

never mind got it is called implementation file .ccp instead of .cpp

lotrsimp12345 37 Posting Pro in Training
Implementation file

using namespace std;
#include "Interface.h"
#include <time.h>



void timer::start()
{
    if(! running)
    {
        begin=(unsigned int) clock();
        running=true;
    }
}

void timer::end()
{
    if(running)
    {
        finish=(unsigned int) clock();
        running=false;
    }
}

int timer::elapsed()
{
    if(running)
    {
        return((unsigned int) clock()-begin);
    }
    else
    {
        return finish-begin;
    }
}

int timer::output(unsigned int seconds)
{
    return seconds >= elapsed();
}




interface file
#ifndef INTERFACE_H_INCLUDED
#define INTERFACE_H_INCLUDED
//#include <time.h>
class timer
{
    public:
    void start();
    void end();
    int elapsed();
    int subtract();
    int add();
    int output(unsigned int seconds);
    private:
    bool running;
    int begin;
    int finish;
};
#endif // INTERFACE_H_INCLUDED


main file

#include <iostream>
using namespace std;
#include "Interface.h"
#include <conio.h>




    int main()
    {
        timer t;
        bool quit = false;
        char choice;
        choice=getch();
        while(! quit)
        {
            t.start();
            t.end();
            cout<<"the time difference is"<<t.elapsed();
        }
        return 0;
    }
jephthah commented: he' +11
lotrsimp12345 37 Posting Pro in Training

Here are my 3 files

interface file

#ifndef INTERFACE_H_INCLUDED
#define INTERFACE_H_INCLUDED
#include <time.h>
class timer
{
    public:
    void start();
    void end();
    int elapsed();
    int subtract();
    int add();
    int output(unsigned int seconds);
    private:
    bool running;
    int begin;
    int finish;
};
#endif // INTERFACE_H_INCLUDED



implementation file
using namespace std;
#include "Interface.h"
#include <time.h>


timer::timer()
{
    running=false;
    begin=0;
}
void timer::start()
{
    if(! running)
    {
        begin=(unsigned int) clock();
        running=true;
    }
}

void timer::end()
{
    if(running)
    {
        finish=(unsigned int) clock();
        running=false;
    }
}

int timer::elapsed()
{
    if(running)
    {
        return((unsigned int) clock()-begin);
    }
    else
    {
        return finish-begin;
    }
}

int timer::output(unsigned int seconds)
{
    return seconds >= elapsed();
}


Test file
#include <iostream>
using namespace std;
#include "Interface.h"
#include <conio.h>
class Test
{
int main()
{
    bool quit = false;
    char choice;

    choice=getch();
    timer t;
    while(! quit)
    {
        t.start();
        t.end();
        cout<<"the time is"<<t.output();
    }
}
}



ERROR::::::No matching call for function 'timer::output()
lotrsimp12345 37 Posting Pro in Training

yea that's in my test class

lotrsimp12345 37 Posting Pro in Training

anyone know how to change it a project into a console project in codeblocks?

lotrsimp12345 37 Posting Pro in Training
#include <iostream>
using namespace std;
#include "Interface.h"


void timer::start()
{
    if(! running)
    {
        begin=(unsigned int) clock();
        running=true;
    }
}

void timer::end()
{
    if(running)
    {
        finish=(unsigned int) clock();
        running=false;
    }
}

int timer::elapsed()
{
    if(running)
    {
        return((unsigned int) clock()-begin);
    }
    else
    {
        return finish-begin;
    }
}

int timer::output(unsigned int seconds)
{
    return seconds >= elapsed();
}

It says undefined reference to '_WinMain@16'

Salem commented: 24 posts, and STILL hasn't figured out code tags :( -7
lotrsimp12345 37 Posting Pro in Training

i got it to work. So i don't need help anymore.

lotrsimp12345 37 Posting Pro in Training

problemlength wasn't declared in this scope

lotrsimp12345 37 Posting Pro in Training

it doesn't like problems.length

lotrsimp12345 37 Posting Pro in Training
#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{


    cout <<"enter the problemset and number""\n";
    //problems represents name and numbers
    string problems;
    string problem_name_and_numbers=problems;
    string numbers;
    char quote;
    int total_length=problems.length();
    //gather name
    if(cin.peek()=='"' || cin.peek() == '\'')
    {
        cin >>quote;
        getline(cin,problems,quote);
        int problemslength=(problems.length()+1);
        int difference=total_length-problemlength;
        numbers=problem_name_and_numbers.substr(problemlength,difference);
    }
    else
    {
        getline(cin,problems);
        int problemslength=(problems.length()+1);
        int difference=total_length-problemlength;
        numbers=problem_name_and_numbers.substr(problemlength,difference);
    }

    //gather problem numbers

    return 0;
}
lotrsimp12345 37 Posting Pro in Training

can't use that class he says only C-string or string class may be used.

lotrsimp12345 37 Posting Pro in Training

thanks for the help. will ask teacher tom.

lotrsimp12345 37 Posting Pro in Training

this doesn't seem to work.
char a=static_cast<char>(beginValue);

lotrsimp12345 37 Posting Pro in Training

apparently that won't work for the same reason.