Gå til innhold

Trenger litt hjelp med noen koder :)


Anbefalte innlegg

Vel jeg har kjøpt boka "accelerated c++" og har lest litt av den, atm så skjønner jeg det meste av det jeg har gjort, har kommet til kapitel 2 og har "skrivd" dette programmet.

 

#include <iostream>
#include <string>

// say waht standard library names we will use
using std::cin;  using std::endl;
using std::cout;	using std::string;

int main ()
{ 
// ask for the persons name
cout << "Please enter your first name: ";

// read the name
string name;
cin >> name;

// build the message we intend to write
const string greeting = "Hello, " + name + "!";

// the number of blanks surrounding the greeting
const int pad =1;

// the number of rows and columns to write
const int rows = pad * 2 + 3;
const string::size_type cols = greeting.size() + pad * 2 + 2;

//write a blank line to seperate the output from the input
cout << endl;

// write rows rows of output
// invariant: we have written r rows so far.
for (int r = 0; r != rows; ++r) {

 string::size_type c = 0;

 	// invariant: we have written c characters so far in the current row.
 	while (c!= cols) {
   
   //it is time to write the greeting?
   if (r == pad + 1 && c == pad + 1) {
   	cout << greeting;
   	c += greeting.size();
   } else {
   	
   	// are we on the border?
   	if (r == 0 || r == rows - 1 || c == 0 || c == cols -1)
     cout << "*";
   	else 
     cout << " ";
   	++c;
   }
 	}

 	cout << endl;
}
return 0;
}

 

Så fikk jeg en oppgave som lyder (Rewrite the framing program to ask the user to supply the amount of spacing to leave between the frame and the greeting)

 

På starten så tenkte jeg at dette kom til å bli umulig men så prøvde jeg meg litt fram, ikke at det funker men kanskje det er i rett retning så jeg trenger litt "guidance" :p

 

dette er det jeg har prøvd meg på:

 

	// ask for how many lines from the greeting to the top and the bottom.
cout << "How many lines do you want from the greeting to the frame?: ";

// read the lines
string number1;
cin >> number1;

// the number of rows and columns to write
const int rows = number1 * 2 + 3;
const string::size_type cols = greeting.size() + number1 * 2 + 2;

 

og herifra så får jeg bare en haug med feilmeldinger.... noen som kan hjelpe meg litt? :hmm:

 

Takk på forhånd :D

Lenke til kommentar
Videoannonse
Annonse

tja har kommet litt lenger:

 

// ask for how many lines from the greeting to the top and the bottom.
cout << "How many lines do you want from the greeting to the frame?: ";

// read the lines
string lol;

const int  pad = lol;

men har istedenfor 100 errors 2 feil :)

 

: error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

: error C2440: 'initializing' : cannot convert from 'std::string' to 'const int'

No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

 

mener at "lol" ikke kan endre seg og vil derfor være konstant ut hele programmet.. bare si ifra hvis jeg er helt på jordet :p

Lenke til kommentar

Hurray :D jeg greide det :)

 

dette er den koden jeg kom fram til :p

 

#include <iostream>
#include <string>

// say waht standard library names we will use
using std::cin;  using std::endl;
using std::cout;	using std::string;

int main ()
{ 
// ask for the persons name
cout << "Please enter your first name: ";

// read the name
string name;
cin >> name;

// build the message we intend to write
const string greeting = "Hello, " + name + "!";

// ask for how many lines from the greeting to the top and the bottom.
cout << "How many lines do you want from the greeting to the frame?: ";

// read the lines
string::size_type lol;
cin >> lol;

const int pad = lol;

// ask for how many spaces from greeting left to right
cout << "How many spaces between the greeting and the frame?: ";

//read the spaces
string::size_type omg;
cin >> omg;


const int cols = greeting.size() + omg * 2 + 2;

// the number of rows and columns to write
const int rows = pad * 2 + 3;


//write a blank line to seperate the output from the input
cout << endl;

string::size_type c = 0;

//write a blank line to seperate the output from the input
cout << endl;

// write rows rows of output
// invariant: we have written r rows so far.
for (int r = 0; r != rows; ++r) {

 string::size_type c = 0;

 	// invariant: we have written c characters so far in the current row.
 	while (c!= cols) {
   
   //it is time to write the greeting?
   if (r == pad + 1 && c == pad + 1) {
   	cout << greeting;
   	c += greeting.size();
   } else {
   	
   	// are we on the border?
   	if (r == 0 || r == rows - 1 || c == 0 || c == cols -1)
     cout << "*";
   	else 
     cout << " ";
   	++c;
   }
 	}

 	cout << endl;
}
return 0;
}

:w00t:

 

men seff det er mer :p

har nå kommet til en oppgave som lyder:

The framing program writes the mostly blanklines that separate the borders from the greeting once character at a time. Change the program so that it writes all the spaces needed in a single output.

 

tja, er ikke helt sikker på hva jeg skal gjøre nå.. noen som kan hjelpe meg litt på vei :) (ikke gi meg koden den vil jeg finne ut selv, vil bare vite hva jeg må gjøre med koden for å få det til og funke)

 

Takk på forhånd:D

Endret av Wolfie
Lenke til kommentar

Du må endre if-setningene dine i while-løkken til å ta seg av litt andre oppgaver.

Tips for rekkefølge er:

if(#print greeting#) {
 cout << greeting;
 ...  
} else {
 if(#border#) {
   cout << "*";
   ...
 } else { //Whitespace
   if(#space#) { //HVis du er på linje med hilsingen
     cout << ' ';
     ...
   } else {
     //Her skal du printe flere mellomrom samtidig. 
     //Tips: string spaces(numSpaces, ' ');
   }
 }
}

Lenke til kommentar

Opprett en konto eller logg inn for å kommentere

Du må være et medlem for å kunne skrive en kommentar

Opprett konto

Det er enkelt å melde seg inn for å starte en ny konto!

Start en konto

Logg inn

Har du allerede en konto? Logg inn her.

Logg inn nå
  • Hvem er aktive   0 medlemmer

    • Ingen innloggede medlemmer aktive
×
×
  • Opprett ny...